home *** CD-ROM | disk | FTP | other *** search
/ Aminet 12 / Aminet 12 (1996)(GTI - Schatztruhe)[!][Jun 1996].iso / Aminet / dev / lang / HeliOS4.lha / helios_demo_disk4 / tutorials / HeliOSLanguageTutorial1.doc < prev    next >
Encoding:
Text File  |  1996-03-07  |  94.1 KB  |  2,581 lines

  1.  
  2. Topics covered in this tutorial:
  3.  
  4. * Looking forward and getting started
  5. * How HeliOS differs from other computer languages
  6. * Controlling the Amiga with HeliOS
  7. * A first HeliOS program
  8. * Rules for creating new command words
  9. * Simple programming strategy
  10. * Order of creation of HeliOS commands
  11. * Organisation and structure
  12. * Using IF...ELSE...THEN constructs in conditional statements
  13. * Combining flags
  14. * Functions, Data and Error Checking
  15. * The HeliOS vocabulary, the dictionary, and the compilation process
  16. * The concepts of RUN-TIME and COMPILE-TIME
  17. * Creating standalone HeliOS programs
  18. * The HeliOS Stack
  19.  
  20. -----------------------------------
  21. Looking forward and getting started
  22. -----------------------------------
  23.  
  24. These introductory tutorials are intended to explain a few general ideas
  25. which may help you get started with HeliOS.
  26.  
  27. Because simple once-only statements of fact are not really the best way of
  28. introducing a complex subject, we have provided a series of tutorials which
  29. in some respects cover similar ground in different ways, thus emphasising
  30. important information and gradually broadening the scope of the examples.
  31.  
  32. In particular, the first two tutorials both approach the first steps in
  33. HeliOS programming from slightly different angles, with many basic concepts
  34. reiterated for emphasis.
  35.  
  36. Hopefully this many-faceted approach will help make some of the more
  37. unfamiliar concepts accessible to most people, and once you are confident
  38. enough to write your own first programs you will be able to pick up further
  39. knowledge directly from the reference material as you need it.
  40.  
  41. Anyway, if you come across something you feel that you already know in the
  42. tutorials, please do continue reading unless you already are very confident
  43. of your understanding, because there may be some new ideas in here.....
  44.  
  45. ------------------------------------------------
  46. How HeliOS differs from other computer languages
  47. ------------------------------------------------
  48.  
  49. Before explaining the fundamentals of HeliOS programming it is worthwhile
  50. looking briefly at the differences between computer programming languages.
  51. This is important because HeliOS is probably very different from any other
  52. languages with which you may be familiar and may well open up many novel
  53. possibilities which you had not previously considered.
  54.  
  55. If you are already experienced with a computer language such as BASIC or
  56. PASCAL, you will need to forget many of the restrictions imposed by such
  57. languages.  HeliOS is a fluid, redefinable, very linguistically oriented
  58. language, with much greater freedom of expression and very different "free
  59. form" programming style.
  60.  
  61. If you are already familiar with assembly language you will, in some ways,
  62. be more instantly at home with HeliOS, because it allows direct control of
  63. data storage in memory in a similar fashion to assembly language.  In fact,
  64. HeliOS allows the integration of assembly language directly within high
  65. level HeliOS code, and HeliOS has many functions which are very close to
  66. assembly language in concept.  HeliOS is also close to assembly language in
  67. potential speed and efficiency.
  68.  
  69. If you are new to programming, you will find that HeliOS will be rather more
  70. easy to learn than some of the more restrictive and old fashioned computer
  71. languages such as BASIC.  Coming fresh to HeliOS with no preconceptions, you
  72. will perhaps be better placed even than already experienced programmers to
  73. take advantage of all the novel new concepts found in HeliOS.
  74.  
  75. Since HeliOS as so different from other computer languages, let us assume
  76. that you are new to programming and take things from the beginning.
  77.  
  78. Your first task is to learn a few simple and fundamental aspects of general
  79. communication with your computer: think of it as learning to converse with
  80. someone who does not speak your own native language.  You will have to learn
  81. a new mode of communication, and interestingly you can also adapt HeliOS to
  82. use a form of linguistic expression chosen by you.
  83.  
  84. Unlike most other computer languages HeliOS is an EXTENSIBLE language, so
  85. you can literally program by extending the actual set of words you use to
  86. communicate with the computer: you effectively build your own dialect of
  87. the language as you go.
  88.  
  89. In a sense you could say that HeliOS allows you to train the computer to
  90. speak YOUR personal language.  This is quite an advanced capability which
  91. allows HeliOS programmers to develop a high degree of personal style and
  92. creativity.
  93.  
  94. Using HeliOS you can actually build a unique language which ideally suits
  95. your own style of programming, and which interfaces to the Amiga in the most
  96. efficient possible way.  HeliOS allows you to create new commands based on
  97. your own ideas and incorporating programming methods which you prefer.
  98.  
  99. Notice how much emphasis is being placed upon "communication" and the use of
  100. "language": this is very important, because HeliOS is really about inventing
  101. your own communication channel with the Amiga, based upon a mutual use of a
  102. shared vocabulary which YOU specify.  You could almost say that HeliOS is a
  103. verbally or semantically oriented language, rather than a simple number
  104. manipulating or "computational" language.
  105.  
  106. Already, if you are a BASIC or "C" programmer, you will perhaps be slightly
  107. intrigued by what is a whole new world of programming possibilities.
  108.  
  109. Of course there are many computer languages, all of which have strengths and
  110. weaknesses, and also the computer which you use will include its own set of
  111. individual features.  Your task is really twofold: to master an effective
  112. means of communication and then to learn how to control all the different
  113. functions of the particular machine with which you are working.
  114.  
  115. Your choice of computer determines the final results which you can achieve,
  116. and some computers are better at certain tasks than others.  The Amiga is
  117. possibly the most versatile home computer of all, so you will certainly have
  118. many interesting avenues to explore.
  119.  
  120. Your choice of language will very much influence the ease and productivity
  121. of the task of programming, and your chosen language might be assessed in
  122. terms of two really vital measures of performance:
  123.  
  124. 1. Is the language capable of flexible control of ALL aspects of your
  125.    computer, or is it going to be incapable of performing certain tasks.
  126.  
  127.    For example, most high level languages lack speed, and are therefore
  128.    less capable than assembly language when it comes to creating games.
  129.    If you want to write game software without having to resort to assembly
  130.    language you need to choose a very fast high level language.
  131.  
  132. 2. Is the language flexible and easy to use from YOUR point of view.
  133.  
  134.    For example, ideally you need a language which makes it easy to do the
  135.    kinds of things you want to do with the computer.  You also need to be
  136.    comfortable with the general interface between you and the language in
  137.    terms of editing source code and program testing.
  138.  
  139. Computer languages vary in speed and power: most languages are called "high
  140. level" languages because they insulate you from dealing with "low level"
  141. technicalities to do with the actual operation of the computer hardware.
  142. These languages usually have a certain trade-off of speed and flexibility
  143. in favour of simplicity of use: they are biased towards making life easy
  144. for the programmer rather than being computer-efficient.
  145.  
  146. At the other end of the programming language scale is assembly language,
  147. which deals directly with the computer hardware and requires the human
  148. programmer to adapt to the needs of the machine rather than vice-versa.
  149. Assembly language is the most powerful language in terms of speed and
  150. efficiency, but it is quite difficult and unforgiving from the point of
  151. view of the programmer.
  152.  
  153. Another broad difference between languages is whether they are "compilers"
  154. or "interpreters".  A "compiler" has to first pre-process a complete set
  155. of program instructions and prepare an executable file which then has to be
  156. run to test the program.  This can be a slow and tedious process, and makes
  157. the task of the beginner so much harder because there is no facility for
  158. easy instant testing of small code fragments.
  159.  
  160. An "interpreter" on the other hand, allows you to type in a command and
  161. get an instant response: the language can instantly interpret what you type.
  162. This is excellent for beginners, because it is easy to test small sections
  163. of code and experiment to get things working correctly.
  164.  
  165. Traditionally interpreters were slow languages because all the program
  166. instructions were literally being interpreted as the program ran.  In
  167. recent times interpretive languages have often been equipped with add-on
  168. compilers which process the final program source code into an "executable"
  169. form which runs faster because it has been "pre-interpreted".  Most more
  170. modern and efficient languages are pure compilers, and the special
  171. advantages of interpretive languages are no longer available to users of
  172. such languages.
  173.  
  174. So, being unrealistic and hopeful, ideally you might be looking for an
  175. easy to learn high level language, which is capable of assembly language
  176. speed, full direct computer hardware control if required, and which has
  177. the best features of both a compiler and an interpreter!
  178.  
  179. You might think that this is an impossible dream.....
  180.  
  181. Certainly this requirement eliminiates virtually all languages available
  182. for the Amiga, but HeliOS actually does fulfill all these functions!
  183.  
  184. HeliOS is a very flexible interpreter AND compiler: a high level language
  185. with a natural speed nearly equivalent to assembly language.  HeliOS has a
  186. full capacity to control the computer hardware directly, and can even allow
  187. pure assembly language to be written within high level code.  Best of all,
  188. HeliOS has the most sophisticated and comfortable "human interface" of ANY
  189. language, so its powerful programming tools are available in an enjoyable
  190. and easily approachable form.
  191.  
  192. HeliOS will easily allow you to exploit every facility of the Amiga, but do
  193. not try to rush into doing complicated things too early.  The Amiga itself
  194. is very complex and sophisticated, and no matter how easily HeliOS allows
  195. you to write programs, you must take care to learn how the Amiga functions
  196. in detail before you can effectively control the computer.
  197.  
  198. This tutorial will teach you how to work with HeliOS, and you will soon
  199. learn to work with the Amiga operating system and hardware: then you will
  200. be free to create the most ambitious programs you can imagine.
  201.  
  202. However, that is for the future, and for now we will look at some simple
  203. ways to get to know the HeliOS language.
  204.  
  205. ---------------------------------
  206. Controlling the Amiga with HeliOS
  207. ---------------------------------
  208.  
  209. How does a computer language allow you to control a computer?  How do YOU
  210. control your chosen computer language?
  211.  
  212. In fact......just how does a simple digital machine manage to "come alive"
  213. and produce the fascinating sounds and graphical extravaganzas which we
  214. often see in Amiga software?
  215.  
  216. It does so, largely, by your creative input, and to provide this you need
  217. tools to enable you to express yourself using this new creative medium.
  218. You then need to learn to use these tools constructively and confidently,
  219. so that you can turn the computer into an instrument for expressing your
  220. own ideas effectively.
  221.  
  222. HeliOS provides you with an excellent toolkit, but if you don't master the
  223. fundamentals well, as in any artistic pursuit, the computer and the language
  224. will impose themselves on you to the point that you are forced to tread a
  225. tedious tightrope of obediance to rigid rules.  HeliOS gives you hundreds of
  226. different ways to do any job: the choice is yours, so learn how to use this
  227. range of choice for your own creative ends.
  228.  
  229. If you want to be in control and have choices on how to perform a certain
  230. programming task, you need to have a good general idea of how the computer
  231. works and a sound understanding of the fundamental operation of your chosen
  232. computer language.  Even though it takes time and experience to accumulate
  233. full understanding of these matters, you should try from the start to learn
  234. as much as possible about the technical background within which framework
  235. your programs function.
  236.  
  237. You will gain experience and detailed knowledge quite easily and painlessly
  238. as you progress, and initially incomprehensible technical details will soon
  239. fall into place if you persevere.  Eventually, when you come to write more
  240. complex software, there is no doubt that you will have to do some learning
  241. about Amiga-specific technicalities, but first of all you need an overview
  242. of the programming process and a few simple ground rules to get you started
  243. with HeliOS.
  244.  
  245. If you follow the simple tutorial exercises and grasp fundamental HeliOS
  246. programming practises, you will be able to enjoy rewarding results from
  247. even your first day of HeliOS programming, without needing too much Amiga
  248. technical knowledge.  HeliOS has many simple commands which will help you
  249. to easily control quite complex Amiga functions, and using these simple
  250. pre-programmed commands as a starting point you will soon be able to write
  251. all kinds of interesting and useful software.
  252.  
  253. The Amiga computer has many functional sub-systems which can be likened
  254. to groups of people with various talents, each of whom uses a specific
  255. linguistic "dialect" designed to express concepts related to their task.
  256. It will be your job as a programmer to communicate with all these "people",
  257. each in their own idiom, and you will need to develop tools and methods
  258. with which to express your instructions.  For example, you would need one
  259. type of knowledge and technique for writing a sound synthesizer program,
  260. and you would need another for writing a graphics-intensive arcade game.
  261.  
  262. An ideal computer language is an "interface" consisting of pre-designed
  263. commands which are comprehensible to the programmer and which are also
  264. adaptable to meet the demands of communicating with every aspect of the
  265. host computer.  HeliOS has many specialised commands to help you deal with
  266. specific functions of the Amiga, and is probably the closest thing to a
  267. total Amiga control language which you will find.
  268.  
  269. Any programming language presents one face to you and another face to the
  270. machine: this is its job.  The less work that has to be done "in between",
  271. in forcing your commands into direct machine instructions, the better
  272. and more efficient will be the result.  HeliOS itself imposes a minimum
  273. of its own character and allows you to control all aspects of the Amiga
  274. very directly, in the way in which you choose.
  275.  
  276. Most programming languages have developed over years into fairly rigid
  277. and limited systems which are not ideally adaptable to all aspects of
  278. controlling modern computers.  This tends to mean that a language has a
  279. very formal structure, and in fact many languages are only good at certain
  280. particular tasks.
  281.  
  282. HeliOS takes a different approach and allows you build your own language for
  283. any programming requirement: HeliOS allows you, the programmer, to recreate
  284. the language itself for any particular task you need to do.
  285.  
  286. In fact it is true to say that as well as being a language in its own right,
  287. HeliOS might also be regarded as a sophisticated set of tools for creating
  288. new computer languages.
  289.  
  290. In HeliOS, if you want to carry out a complex task, you first have to break
  291. down the task into sensible small units, then you EXTEND the language by
  292. CREATING new commands to do the particular things you require.
  293.  
  294. Your new commands are named however YOU choose, so they instantly make
  295. sense to you and you remember them easily.  In other languages you are
  296. required always to remember fixed commands written by someone else, and
  297. to conform to rigidly inflexible command sets.
  298.  
  299. HeliOS, in short, provides you with a flexible and open ended way of
  300. developing your knowledge of the Amiga and your power to control it.
  301.  
  302. ----------------------
  303. A first HeliOS program
  304. ----------------------
  305.  
  306. Let us take, as a first HeliOS programming task, the traditional computer
  307. language introductory program: getting the computer to say "Hello World!".
  308.  
  309. To do this in "C" you would need to write quite a lot of code to create a
  310. stand-alone program which would first need to set up the capacity for the
  311. computer to display a screen and to display text on this screen.
  312.  
  313. In HeliOS, even to write a "stand alone" program to do this would only be
  314. a matter of a few words: you could actually do it in one line of code!
  315.  
  316. However, HeliOS includes an "Interpreter" which already has a text display
  317. ready for use, so for a simple example all you need to do is switch to the
  318. HeliOS Interpreter screen and type your commands directly.
  319.  
  320. Like BASIC, HeliOS has simple commands to output text, but unlike BASIC
  321. HeliOS also has a very wide range of more sophisticated and powerful text
  322. commands to do many special operations.  However, for now, all we need use
  323. is a very simple text output function, and this can be done in HeliOS using
  324. the expression:
  325.  
  326.                        ." Hello World!"
  327.  
  328. Let us examine this simple expression, and firstly we can look at the
  329. initial two-character word at the start:
  330.  
  331.                        ."
  332.  
  333. The first part, the . character, is used by HeliOS to indicate that
  334. something is being output: it is roughly equivalent to a shorthand form
  335. of the word "print".
  336.  
  337. The second part, the " character, is used to indicate that this "double
  338. quote mark" will be used to "delimit", or "enclose" the text we want
  339. to print.
  340.  
  341. This expression as a whole is saying, in a cryptic form, "print the text
  342. following enclosed by double quote marks to the screen".
  343.  
  344. In computer parlance we have a special name for the double quote character
  345. used to indicate the start and end of our text message: we call this a
  346. DELIMITER, because it "delimits" a text message surrounded by other words
  347. which have a different function.
  348.  
  349. Notice that we have emphasised the need to "enclose" text messages within
  350. delimiters: we cannot simply say:
  351.  
  352.                           Hello World!
  353.  
  354. or even
  355.  
  356.                        ." Hello World!
  357.  
  358. without the final quotation mark.
  359.  
  360. This is important!
  361.  
  362. When you are telling the computer to do something with text you need to be
  363. able to define where the text starts and stops, so that the computer can
  364. tell the difference between COMMAND words and the actual words of your
  365. embedded TEXT message.
  366.  
  367. To understand this, imagine that you had typed a set of commands at the
  368. Interpreter command line, and that one of these commands was an instruction
  369. to print some text to the screen.
  370.  
  371. You might have something like this:
  372.  
  373. COMMAND1 COMMAND2 ." This is my text to print" COMMAND3 COMMAND4 etc.
  374.                    ^                         ^
  375.               First delimiter       Second delimiter
  376.               = START of text       = END of text
  377.  
  378. You can see how the two " characters are used to enclose and distinguish
  379. the embedded text message from the commands before and after them.
  380.  
  381. This concept of "delimiters" is one you will meet often when using text
  382. embedded within program code.
  383.  
  384. Let us go back now to our "Hello World!" program:
  385.  
  386.                        ." Hello World!"
  387.  
  388. If you type this at the HeliOS command line, the Amiga will output the
  389. text message "Hello World!".  It is as simple as that, so why not try it
  390. now.....
  391.  
  392. Here you have typed in one single direct command, but you can of course
  393. type in a sequence of commands to do something a little more complicated.
  394.  
  395. Try typing the following (actually, you do not need to retype every word,
  396. just press "up-arrow" to recall your previous line of text and then edit
  397. the line as required):
  398.  
  399.   SCRCLR  ." Hello World!" WAITSPACE
  400.  
  401. This will do three different things, one after the other:
  402.  
  403. 1. Clear the text screen
  404. 2. Print "Hello World!"
  405. 3. Wait for you to press space.
  406.  
  407. Note that all computer programs are essentially simple linear sequences of
  408. operations, because the computer can actually only perform one function
  409. at any one time.
  410.  
  411.  
  412. Now try typing the following:
  413.  
  414. SCRCLR 12 12 CURPUT ." Hello World!"  WAITSPACE
  415.  
  416. This will set the text cursor position at "column 12, row 12" on the
  417. text screen before printing your message.
  418.  
  419. You have already written a short HeliOS program, but it is not very
  420. convenient to keep typing all this in at the command line every time.
  421.  
  422. We are now going to create a new single word command which will become
  423. a part of the HeliOS vocabulary.  This single word will the carry out all
  424. the commands in your small new program: effectively you are writing the
  425. program for a new HeliOS command word.
  426.  
  427. To create a new HeliOS command word we use two HeliOS functions expressed
  428. by use of the colon and semi-colon characters.  The colon character tells
  429. HeliOS that the next word will be the name of a new command, followed
  430. by a command definition.  The semi-colon character tells HeliOS that the
  431. new command definition is finished.
  432.  
  433. Let us create a new word called HELLO.
  434.  
  435.  :        HELLO      SCRCLR  12 12 CURPUT  ." Hello World!"  WAITSPACE   ;
  436.  ^        ^^^^^      <----------------------------------------------->   ^
  437.  Start    Name                        Definition                        End
  438.  
  439.  
  440. This has created a new word called HELLO.
  441.  
  442. Try entering this command line and then displaying the USER vocablulary.
  443.  
  444. You will see your new word HELLO listed as a HeliOS command word: you have
  445. already extended the HeliOS language and written a small program.
  446.  
  447. Now try typing just the word HELLO at the command line and press <Return>.
  448.  
  449. You will see that your new word does exactly what the original set of
  450. commands did when typed in individually: you have just extended the HeliOS
  451. language with a new command named and designed to your own specification.
  452.  
  453.  
  454. ------------------------------------
  455. Rules for creating new command words
  456. ------------------------------------
  457.  
  458. Here, then, are the simple rules for creating new HeliOS command words:
  459.  
  460. 1. Place a "colon" (":") symbol with at least one space on each side.
  461.  
  462. 2. Put your new command name, followed by at least one space.
  463.  
  464. 3. Put your list of "internal" commands with spaces separating them.
  465.  
  466. 4. Place a "semicolon" (";") symbol with at least one space on each side.
  467.  
  468.  
  469. We refer to a word definition such as this as a "colon definition".
  470.  
  471. Note that the colon definition can occupy as many lines as you like: there
  472. is no limit on its size and complexity.
  473.  
  474. This process of creating new commands is exactly what you do when writing
  475. all HeliOS programs: you keep on extending the language with ever more
  476. powerful commands, until you finally have one "master" command which uses
  477. all your previous constructions to carry out the main program.
  478.  
  479. Let us now make another command, called "PRINT_RED".
  480.  
  481.  : PRINT_RED 6 FPENSET ;
  482.  
  483. This command sets the text colour to "colour 6", which will be RED unless
  484. you have changed your HeliOS colours.
  485.  
  486. You can now say:
  487.  
  488.   PRINT_RED HELLO
  489.  
  490. Getting more ambitious, we can make another new word:
  491.  
  492.  : PRINT_RED_HELLO   PRINT_RED HELLO ;
  493.  
  494. This is a compound word made up from our previous new commands, and you
  495. will see that HeliOS can freely mix any combination of your new words with
  496. its own CORE command set.
  497.  
  498. Remember: 
  499.  
  500. * There is no essential difference between HeliOS CORE functions and the 
  501.   command words which you create yourself using colon definitions.
  502.  
  503. Can you see what the following expression does?
  504.  
  505.  : PRINT_BOLD_RED_HELLO   BOLDON PRINT_RED_HELLO ;
  506.  
  507. Try it!
  508.  
  509. And this....
  510.  
  511.  : FANCYHELLO   7 BPENSET PRINT_BOLD_RED_HELLO 2 BPENSET SCRCLR ;
  512.  
  513. The process of building new HeliOS commands should now be quite clear.
  514.  
  515. When you come to write a "real" program of course, you will not be using
  516. the command lines: you will be writing programs as text (or "source code")
  517. in an editor.
  518.  
  519. Many computer languages require source code to be written in a very strict
  520. format, but HeliOS is totally relaxed in its source code format, allowing
  521. you to choose a style which suits your own preference.
  522.  
  523. It is a good idea to use plenty of "white space" so that your programs
  524. are not cluttered and confusing, and it is best to make use of separate
  525. lines for distinct command sequences.
  526.  
  527. Look at this example of how you might turn all the above commands into
  528. a short HeliOS program in a text editor:
  529.  
  530.  : HELLO
  531.  
  532.  SCRCLR
  533.  12 12 CURPUT
  534.  ." Hello World!"
  535.  WAITSPACE
  536.  ;
  537.  
  538.  : PRINT_RED
  539.  
  540.  6 FPENSET
  541.  ;
  542.  
  543.  : PRINT_RED_HELLO
  544.  
  545.  PRINT_RED
  546.  HELLO
  547.  ;
  548.  
  549.  : PRINT_BOLD_RED_HELLO
  550.  
  551.  BOLDON
  552.  PRINT_RED_HELLO
  553.  ;
  554.  
  555.  : FANCYHELLO
  556.  
  557.  7 BPENSET
  558.  PRINT_BOLD_RED_HELLO
  559.  2 BPENSET
  560.  SCRCLR
  561.  ;
  562.  
  563. We have used UPPER CASE characters throughout, but you do not need to do so.
  564.  
  565. You could equally well have written FANCYHELLO as:
  566.  
  567.  : FancyHello
  568.  
  569.  7 BPenSet
  570.  Print_Bold_Red_Hello
  571.  2 BPenSet
  572.  ScrClr
  573.  ;
  574.  
  575. or even:
  576.  
  577.  : FancyHello   7 BPenSet  Print_Bold_Red_Hello  2 BPenSet  ScrClr  ;
  578.  
  579. This last option is actually quite a BAD idea, and with a little thought
  580. we can see why.
  581.  
  582. It is always best to use the sequence of vertically spaced short lines
  583. within your source code to help express the sequential logic of the program.
  584. Keeping each short section of the program on a separate line makes it easy
  585. to follow what the program is doing and also leaves space to add comments
  586. on individual operations as required.  The layout of your source code can,
  587. and should, help you comprehend the structure of your program.
  588.  
  589. It is very useful in many cases to add notes to your programs, explaining
  590. what is happening at each stage.  This is called "commenting", and the
  591. addition of "comments" is vitally important once you start writing big
  592. complicated programs.  You may understand what a program is doing when you
  593. write it, but you will surely forget details when you return to it after
  594. some time has elapsed.
  595.  
  596. It is often very useful to add comments to each line, like this:
  597.  
  598.  : FancyHello                     \ Print red and yellow "Hello" message
  599.  
  600.  7 BPenSet                        \ Set background to "yellow"
  601.  Print_Bold_Red_Hello             \ Clear display and print message
  602.  2 BPenSet                        \ Reset background to white
  603.  ScrClr                           \ Clear display
  604.  ;
  605.  
  606. Notice that we use the "\" character to inform HeliOS that all text on the 
  607. rest of that line is a comment.
  608.  
  609. HeliOS source code can also include longer multi-line comments.  You do
  610. this by enclosing the long text comment in brackets, like this:
  611.  
  612.  HELLO
  613.  
  614.  ( This is a long text comment, which can go on and on for as many lines
  615.   as I like.  I can carry on writing and HeliOS will ignore all this until
  616.   it finds a final ")" character with spaces on each side of it, upon which
  617.   it will carry on interpreting the program. )
  618.  
  619.  FANCYHELLO
  620.  
  621. Perhaps you might have noticed something quite subtle: there was another ")"
  622. bracket character WITHIN the comment which somehow HeliOS was clever enough 
  623. to ignore.
  624.  
  625. How do you think HeliOS knew which bracket to use to determine the end of
  626. the comment?
  627.  
  628. The answer to this brings us to an important rule:
  629.  
  630. * ALL HELIOS COMMAND WORDS MUST BE DELIMITED BY SPACES
  631.  
  632. This is very natural really, since it is just how we ourselves communicate.
  633.  
  634. After all, look what happens if we remove spaces from the line above:
  635.  
  636. Thisisverynaturalreally,sinceitisjusthowweourselvescommunicate.
  637.  
  638. HeliOS, like any normal person, requires its communications to be separated
  639. by spaces, and because this is such a natural consideration, it is quite
  640. unlikely that you will make the mistake of forgetting it.  However, if you
  641. do miss out a space between two words, HeliOS will merely tell you that
  642. it cannot understand and point out to you where it lost track of what you
  643. were saying.
  644.  
  645. Try typing this at the command line:
  646.  
  647.  : FancyHello   7BPenSet  Print_Bold_Red_Hello  2 BPenSet ScrClr  ;
  648.                 ^^
  649.                 Missing space
  650.  
  651.  
  652. You can see that HeliOS is really quite helpful, and pointed out where you
  653. had gone wrong without any untoward problems.
  654.  
  655. Going back to the case of the bracket within the comment, and indeed to
  656. comment markers in general, you should always remember that after the first
  657. bracket (which must have surrounding spaces) HeliOS will regard everything
  658. as a comment until it finds a final bracket with surrounding spaces.  If
  659. you were to place a ) character surrounded by spaces WITHIN your comment,
  660. HeliOS would assume that you wanted the comment to end there.
  661.  
  662. You should also take care not to write a line like this:
  663.  
  664.  7 BPenSet                        \Set background to "yellow"
  665.                                   ^^
  666.                                No space
  667.  
  668. If you miss out the space, HeliOS will not understand that you are writing
  669. a comment.  Try it and see......
  670.  
  671. One final point will lead us to the conclusion of this first lesson.
  672.  
  673. Perhaps you have noticed already that HeliOS will allow you to make several
  674. new definitions of the same command.  In this case HeliOS warns you that
  675. you have made multiple definitions, but will accept the new command on top
  676. of the previous ones.  Notice that the latest verson defined will be the one
  677. which is performed when you subsequently type the command name.
  678.  
  679. We will discuss this in more detail elsewhere, but for now we might point
  680. out that if you want to clear all old command definitions you simply type:
  681.  
  682.   FORGET **CORE**
  683.  
  684. This will cause HeliOS to "forget" all the new commands it has learned,
  685. and revert to its own in-built set of CORE functions.
  686.  
  687. Notice that once again HeliOS behaves in a fairly "human" way compared with
  688. most computer languages:  you teach it new words and use them in expressive
  689. ways similar to our own use of language, then you tell it to "forget".
  690.  
  691. ***************************************************************************
  692.  
  693. ---------------------------
  694. Simple programming strategy
  695. ---------------------------
  696.  
  697. We can now go on to a slightly more complex example, and in doing so we
  698. will look at simple ways of designing programs more efficiently.
  699.  
  700. Let us say that we want to print to the screen our name and address.
  701.  
  702. If we asked a person to do this type of task, we would assume that they
  703. knew how to write, how to use a pen, etc. etc., and we would be able to
  704. give them one simple command to do the job.
  705.  
  706. A computer on the other hand always has to be told explicitly what to
  707. do at each stage of the process.
  708.  
  709. The art of writing good computer software lies very much in developing
  710. a skillful understanding of how to break down complex tasks into several
  711. simple ones.  A good programmer can reduce a complex task to a relatively
  712. "intuitive" set of economical functions, whereas a less skilled programmer
  713. would probably write twice as much code to do the same job in a clumsy way.
  714.  
  715. All programs ultimately resolve down to the use of the "primitive" commands
  716. available in the computer language you are using, and you need to become
  717. adept at using these commands and skilled in selecting appropriate methods
  718. for undertaking particular tasks.
  719.  
  720. HeliOS has very many preset commands to carry out common tasks, and one of
  721. the first things you must learn is how to find out information about these
  722. standard command words.  The list of command words, in HeliOS, is called the
  723. DICTIONARY, and you will find that the most important file in the whole of
  724. the HeliOS documentation is the "Dictionary.doc" command definition file.
  725.  
  726. You may like to keep a copy of the "Dictionary.doc" file available at all
  727. times by loading it into a spare HeliOS editor.  Alternatively, you could
  728. keep a copy of the file loaded into a separate text viewer program, which
  729. could be multitasked alongside HeliOS on its own "pop-up" screen.
  730.  
  731. By the way, the actual current set of available commands is referred to in
  732. HeliOS as the VOCABULARY, so you can see from all this "verbal" terminology
  733. that this is indeed a very linguistically oriented language.
  734.  
  735. As you write programs you actually add commands of your own to the HeliOS
  736. vocabulary, thus effectively building your own personalised version of the
  737. language.
  738.  
  739. * Remember here that HeliOS has a Vocabulary Help system which is designed
  740.   to allow you to set up short on-line help for all your own newly created
  741.   HeliOS command words.  You can make a short Vocabulary Help entry for each
  742.   new word you create (and for any HeliOS CORE word) so that you then have
  743.   instant on-line help available by simply placing the cursor over the word
  744.   and evoking the Vocabulary Help system.
  745.  
  746. Going back to our example, let us assume we want to print this address:
  747.  
  748. Mr. W. Smith
  749. 12, Blitter Street
  750. AmigaTown
  751. ComputerLand
  752.  
  753.  
  754. Obviously, no computer language comes with a single built-in command which
  755. with one simple action will print your name and address at a chosen place
  756. on the screen and in chosen text colours and styles.  Even this very simple
  757. task requires a program to be written to do it, and our first task must be
  758. to work out a series of simple sub-commands to tell the computer how get the
  759. job done.  Then we can create a new master-command, which we might perhaps
  760. call "PrintAddress", which performs the whole of our new command program.
  761.  
  762. Let us first make a preliminary list of what sort of things we want this
  763. command to do, in the form of a simple very explicit step-by-step sequence:
  764.  
  765. 1.  Clear the page of previous text.
  766.  
  767.     On a computer, remember, the screen/page is not always empty!
  768.  
  769. 2.  Move the pen (or text cursor) to a certain place on the page.
  770.  
  771.     Planning ahead, it would be nice to be able to specify this position
  772.     as a parameter to our new command PrintAddress, allowing us to use the
  773.     command flexibly to print at at different places on the screen.
  774.  
  775. 3.  Set up a text style for the first line of the address (the name)
  776.  
  777.     We will not bother with fonts at this stage, but it would be nice to
  778.     be able to specify text style and colour: for this example perhaps we
  779.     might use BOLD type in black for the first "name" line.
  780.  
  781. 4.  Print our name.
  782.  
  783. 5.  Move to the next line immediately below the start of the name.
  784.  
  785. 6.  Set up a text style for the second part (the address itself)
  786.  
  787.     It would be nice to be able to specify a different text style and
  788.     colour for the address, and for this example we will use ITALIC type
  789.     in black for this part of the text.
  790.  
  791. 7.  Print the first line of the address.
  792.  
  793. 8.  Move to the next line immediately below......
  794.  
  795. 9.  Set text style.
  796.  
  797.     We may or may not want to change styles again, but we include this
  798.     line just in case.
  799.  
  800. 10. Print the second line of the address.
  801.  
  802. 11. Move to the next line immediately below......
  803.  
  804. 12. Set text style.
  805.  
  806.     We may or may not want to change styles again, but we include this
  807.     line just in case.
  808.  
  809. 13. Print the third line of the address.
  810.  
  811. Notice that we need to break down commands to the computer into very simple
  812. sequential operations.
  813.  
  814. We now need to specify descriptive names for all the sequential operations.
  815.  
  816. It is best to always use function names which describe clearly to anyone
  817. reading the code (or yourself in time to come) what is happening.  Using
  818. cryptic and complex looking expressions may make your code look very clever
  819. and difficult, but it is not good programming.  Do not fall into the trap
  820. of using short and meaningless function names: it merely makes your code
  821. hard to read and to debug.
  822.  
  823. 1.  ClearScreen
  824.  
  825. 2.  SetNamePosition
  826.  
  827. 3.  SetNameTextStyle
  828.  
  829. 4.  PrintName
  830.  
  831. 5.  SetAdd1Position
  832.  
  833. 6.  SetAdd1TextStyle
  834.  
  835. 7.  PrintAddress1
  836.  
  837. 8.  SetAdd2Position
  838.  
  839. 9.  SetAdd2TextStyle
  840.  
  841. 10. PrintAddress2
  842.  
  843. 11. SetAdd3Position
  844.  
  845. 12. SetAdd3TextStyle
  846.  
  847. 13. PrintAddress3
  848.  
  849. Having arrived at this stage we can string together these operations into
  850. a program sequence which will define our new address print function.
  851.  
  852. Our new mini-program, PrintAddress, will look something like this:
  853.  
  854.  : PrintAddress
  855.  
  856.     ClearScreen
  857.     SetNamePosition
  858.     SetNameTextStyle
  859.     PrintName
  860.     SetAdd1Position
  861.     SetAdd1TextStyle
  862.     PrintAddress1
  863.     SetAdd2Position
  864.     SetAdd2TextStyle
  865.     PrintAddress2
  866.     SetAdd3Position
  867.     SetAdd3TextStyle
  868.     PrintAddress3
  869.   ;
  870.  
  871. Notice that this is still just a simple sequential list of commands.
  872.  
  873. You can imagine issuing these commands to a person (perhaps being a little
  874. more polite), and then watching the sequential series of commands being
  875. performed.
  876.  
  877. It is exactly the same with the computer, which will perform the commands
  878. making up the PrintAddress function in simple linear order.
  879.  
  880. Before we start issuing these commands we really ought to think a little
  881. to see if we have decided on the best way of doing things.  It is all very
  882. well instructing people or computers to do things, but it does make a great
  883. deal of difference if the commands which we issue are sensible. In the case
  884. of people, this makes them much happier to carry out our plans, and with
  885. computers we usually get the job done quicker and much more efficiently if
  886. we think things out carefully first.
  887.  
  888. Look again at our command list:
  889.  
  890.  
  891.               ClearScreen
  892.               SetNamePosition
  893.               SetNameTextStyle
  894.               PrintName
  895.               SetAdd1Position    *
  896.               SetAdd1TextStyle   +
  897.               PrintAddress1
  898.               SetAdd2Position    *
  899.               SetAdd2TextStyle   +
  900.               PrintAddress2
  901.               SetAdd3Position    *
  902.               SetAdd3TextStyle   +
  903.               PrintAddress3
  904.  
  905. Look at the lines marked with * and +.
  906.  
  907.  
  908. Now look again at another possible version of this program:
  909.  
  910.         1     ClearScreen
  911.         2     SetNamePosition
  912.         3     SetNameTextStyle
  913.         4     PrintName
  914.         5     NextScreenLine     *
  915.         6     SetAddTextStyle    +
  916.         7     PrintAddress1
  917.         8     NextScreenLine     *
  918.         9     SetAddTextStyle    +
  919.         10    PrintAddress2
  920.         11    NextScreenLine     *
  921.         12    SetAddTextStyle    +
  922.         13    PrintAddress3
  923.  
  924. Notice that in the new version lines 5, 8, and 11 are all the same command,
  925. which we call NextScreenLine, and lines 6, 9 and 12 are also all the same
  926. command SetAddTextStyle.
  927.  
  928. This means that we will be able to re-use parts of our program, which will
  929. save time and effort: notice that we need less different command words in
  930. the new definition.
  931.  
  932. We have managed to do this because we noticed that parts of the task could
  933. be described in a way which was repetitive and similar:
  934.  
  935. 1. Setting a new line can be done by just moving down the page by one line.
  936.  
  937. 2. We can use the same address text style for each line of the address.
  938.  
  939. This kind of strategic planning is a very important part of designing all
  940. computer software.
  941.  
  942. Now we have reduced the number of DIFFERENT commands we need to create
  943. from 13 to 9, but we can go further still:
  944.  
  945.         1     ClearScreen
  946.         2     SetNamePosition
  947.         3     SetNameTextStyle
  948.         4     PrintName
  949.         5     NextScreenLine
  950.         6     SetAddTextStyle
  951.         7     PrintAddress1
  952.         8     NextScreenLine
  953.         9     PrintAddress2
  954.         10    NextScreenLine
  955.         11    PrintAddress3
  956.  
  957. Here we still have 9 different commands, but we have removed the two last
  958. instances of "SetAddTextStyle" from our final program.  We can do this
  959. only because we know that once a text style is set in HeliOS, any further
  960. text will continue to be printed in that style.
  961.  
  962. We needed SPECIAL KNOWLEDGE to make this particular economy, and you will
  963. find that as you grow in experience you will on many occasions be able to
  964. make your programs more economical by "tricks" such as this.
  965.  
  966. We could go on making changes, but let us for now settle on this sequence
  967. of commands for our program.
  968.  
  969.  : PrintAddress
  970.  
  971.    ClearScreen
  972.    SetNamePosition
  973.    SetNameTextStyle
  974.    PrintName
  975.    NextScreenLine
  976.    SetAddTextStyle
  977.    PrintAddress1
  978.    NextScreenLine
  979.    PrintAddress2
  980.    NextScreenLine
  981.    PrintAddress3
  982.   ;
  983.  
  984.  
  985. Remember that we COULD write this as:
  986.  
  987.  : PrintAddress    ClearScreen SetNamePosition SetNameTextStyle PrintName
  988.  NextScreenLine SetAddTextStyle PrintAddress1 NextScreenLine PrintAddress2
  989.  NextScreenLine PrintAddress3 ;
  990.  
  991. Notice that this looks much more confused and is harder to read.
  992.  
  993. As we said above, because computers are sequential devices operating on
  994. one command at a time, it is most useful to write programs with one command
  995. per line.  Using this method it is usually easier to see what is happening.
  996.  
  997. If all the sub-commands in PrintAddress were HeliOS "CORE" words we could
  998. proceed simply to write the program as expressed above.  However, looking
  999. at our definition of PrintAddress, we can see that HeliOS is going to need
  1000. to know what we mean by several NEW sub-commands before it can carry out
  1001. our instructions.  For example, we have not yet told HeliOS how to
  1002. "ClearScreen", etc.
  1003.  
  1004. If we tried to compile this definition as it stands HeliOS would be unable
  1005. to understand words such as "ClearScreen" and would register an error.
  1006.  
  1007. So, we now need to define all our new sub-command words, one at a time.
  1008.  
  1009. Let us do them in order, with "ClearScreen" first.
  1010.  
  1011. Look at this:
  1012.  
  1013.  :  ClearScreen
  1014.  
  1015.  SCRCLR
  1016.  ;
  1017.  
  1018. You can see that ClearScreen has been defined as just one HeliOS CORE
  1019. command called SCRCLR.
  1020.  
  1021. Actually, we are quite fortunate that HeliOS has this single word SCRCLR to
  1022. clear a text screen: this means that our first definition is very simple!
  1023.  
  1024. The definition of ClearScreen above will create a new command word in the
  1025. HeliOS vocabulary called "ClearScreen", which actually does just the same
  1026. as SCRCLR.  All we have achieved in this case is to make our new name for
  1027. an existing word, but this is quite legitimate.  Whenever we now type
  1028. "ClearScreen", either at the command line or in a program, HeliOS knows
  1029. that we are wanting it to clear a text screen using its own command SCRCLR.
  1030.  
  1031. Actually, you can use this technique to rename any of the CORE HeliOS
  1032. command words if you like.....
  1033.  
  1034. Let's now look at our second command:
  1035.  
  1036.  : SetNamePosition                  ( Column, Row - - - )
  1037.  
  1038.  CURPUT
  1039.  CURSAVE
  1040.  ;
  1041.  
  1042. This one is a little more interesting.
  1043.  
  1044. We here have two HeliOS internal commands which do the job we require:
  1045. CURPUT will set up the text cursor position and CURSAVE will automatically
  1046. remember the cursor position for future reference.
  1047.  
  1048. The HeliOS word CURPUT requires us to tell it where we want the cursor
  1049. putting, which is quite reasonable, and we do this by simply writing the
  1050. column and row position values as numbers before the command like this:
  1051.  
  1052. 20 20 CURPUT
  1053.  
  1054. This would place the cursor at column 20 row 20.
  1055.  
  1056. Quite easy!
  1057.  
  1058. The two numbers required by CURPUT to do its work are called "parameters",
  1059. and the process of supplying these numbers is called "passing parameters".
  1060.  
  1061. You will soon see that HeliOS command words can pass parameters between
  1062. themselves too, with one word passing its result to another, and so on.
  1063.  
  1064. Notice, if you are already a programmer in other languages, that HeliOS
  1065. requirs no definitions of "variables" at this stage: you simple place the
  1066. numerical parameters immediately before the command.
  1067.  
  1068. In fact, HeliOS uses a "Stack" for much of its parameter passing, and for
  1069. now we will simply mention that a stack is a general storage place where
  1070. any parameters can be stored until they are needed later.
  1071.  
  1072. When a HeliOS word requires parameters (in this case Column and Row), we
  1073. have a conventional way of expressing this in the form of a diagrammatic
  1074. representation which we call a "Stack Diagram".  The Stack Diagram of any
  1075. HeliOS word is simply a method of specifying the status of the stack before
  1076. and after that word executes.  This information tells us whether the word
  1077. requires parameters on the stack before it starts, or leaves parameters on
  1078. the stack when it is finished.
  1079.  
  1080. You perhaps noticed this in the definition of SetNamePosition:
  1081.  
  1082.  : SetNamePosition                  ( Column, Row - - - )
  1083.                                     ^^^^^^^^^^^^^^^^^^^^^
  1084.                                        Stack Diagram
  1085.  
  1086. Here is a generalised version of this stack diagram:
  1087.  
  1088.     ( Parameter1, Parameter2  - - - )
  1089.  
  1090. This tells us that the command in question takes two parameters and does
  1091. not itself return any parameters.
  1092.  
  1093. Look at another general example of a Stack Diagram:
  1094.  
  1095.     ( Parameter1, Parameter2   - - -   Parameter3, Parameter4 )
  1096.           ^           ^        <--->       ^           ^
  1097.        1st Input  2nd Input   Command  1st Result  2nd Result
  1098.        parameter  parameter            parameter   parameter
  1099.  
  1100. The "- - -" represents the operation of the command, the parameters on
  1101. the left represent things passed to the command (on the stack) before it
  1102. operates, and parameters returned by the command are shown on the right.
  1103.  
  1104. You might have a word which takes two parameters and then returns one
  1105. parameter itself.  For example "+" takes two numbers, adds them, and
  1106. returns the sum of the two numbers as its result.
  1107.  
  1108. So, "+" would have the following stack diagram:
  1109.  
  1110.     ( Parameter1, Parameter2  - - - Parameter3 )
  1111.  
  1112. or  ( Number1, Number2 - - - Result_Of_Addition )
  1113.  
  1114.  
  1115. Going back to CURPUT, which takes two "column and row" parameters, we can
  1116. notice something interesting: by putting CURPUT into our new command word
  1117. "SetNamePosition", this new word will also require the two column and row
  1118. parameters just like CURPUT.
  1119.  
  1120. This is easy to understand, if you realise that when HeliOS interprets the
  1121. SetNamePosition command, what it actually does is to perform the compiled
  1122. sub-commands CURPUT and CURSAVE sequentially.
  1123.  
  1124. HeliOS says to itself "Ah, what do I do for SetNamePosition?  First I must
  1125. perform the CURPUT command, then the CURSAVE command".
  1126.  
  1127. So, "SetNamePosition" will require the same two parameters as CURPUT (in
  1128. fact the two input parameters are actually used by CURPUT), and will have
  1129. the following "Stack Diagram":
  1130.  
  1131.                      ( Column, Row - - - )
  1132.  
  1133. just like CURPUT.
  1134.  
  1135. You will see later how this works when you run the program.
  1136.  
  1137. Let's see how we are getting on with our command list.
  1138.  
  1139.  : PrintAddress
  1140.  
  1141.    ClearScreen           - done
  1142.    SetNamePosition       - done
  1143.    SetNameTextStyle
  1144.    PrintName
  1145.    NextScreenLine
  1146.    SetAddTextStyle
  1147.    PrintAddress1
  1148.    NextScreenLine
  1149.    PrintAddress2
  1150.    NextScreenLine
  1151.    PrintAddress3
  1152.  ;
  1153.  
  1154. OK. Now let us make "SetNameTextStyle", something like this:
  1155.  
  1156.  : SetNameTextStyle
  1157.  
  1158.  1 FPENSET
  1159.  BOLDON
  1160.  ;
  1161.  
  1162. This time we use two more HeliOS sub-commands to make our new word.
  1163.  
  1164. The command FPENSET takes one parameter, which defines the colour of the 
  1165. foreground text "pen".  In this case we set the text colour to "1", which
  1166. is BLACK.
  1167.  
  1168. Can you work out a stack diagram for FPENSET?
  1169.  
  1170. Here it is:     FPENSET    ( Pen - - - )
  1171.  
  1172. The command BOLDON switches HeliOS text mode to BOLD
  1173.  
  1174. You can see that SetNameTextStyle does not require any external parameters
  1175.  
  1176.  
  1177. The next command we need is "PrintName".
  1178.  
  1179.  : PrintName
  1180.  
  1181.  ." Mr. W. Smith"
  1182.  ;
  1183.  
  1184. This will print the text to the screen in the current text style and
  1185. colour.
  1186.  
  1187. Here we use again the previous simple method of including text in your
  1188. program with the ." command (there are many other more sophisticated ways
  1189. of doing this, as you will see later).
  1190.  
  1191. Let's look at our command list again.
  1192.  
  1193.  : PrintAddress
  1194.  
  1195.    ClearScreen           - done
  1196.    SetNamePosition       - done
  1197.    SetNameTextStyle      - done
  1198.    PrintName             - done
  1199.    NextScreenLine
  1200.    SetAddTextStyle
  1201.    PrintAddress1
  1202.    NextScreenLine
  1203.    PrintAddress2
  1204.    NextScreenLine
  1205.    PrintAddress3
  1206.  ;
  1207.  
  1208. Now we need:
  1209.  
  1210.  : NextScreenLine
  1211.  
  1212.  CURSET
  1213.  1 CURDN
  1214.  CURSAVE
  1215.  ;
  1216.  
  1217. This time we have three HeliOS commands.
  1218.  
  1219.  CURSET   - Restores the first CURSOR position we set.
  1220.  1 CURDN  - Moves the cursor down by one position.
  1221.  CURSAVE  - Saves the new cursor position.
  1222.  
  1223. Notice that we have specially designed this command so that it can be used
  1224. repetitively.  It was this "design feature" which allowed us to use this
  1225. one command in place of three separate commands in our "first version".
  1226.  
  1227. The next command, "SetAddTextStyle", is very similar to "SetNameTextStyle".
  1228.  
  1229.  : SetAddTextStyle
  1230.  
  1231.  BOLDOFF
  1232.  ITALON
  1233.  ;
  1234.  
  1235.  BOLDOFF - Switches off BOLD text.
  1236.  ITALON  - Switches on ITALIC text.
  1237.  
  1238. The commands "PrintAddress1" etc. are all very like "PrintName".
  1239.  
  1240.  : PrintAddress1
  1241.  
  1242.  ." 12, Blitter Street"
  1243.  ;
  1244.  
  1245.  : PrintAddress2
  1246.  
  1247.  ." AmigaTown"
  1248.  ;
  1249.  
  1250.  : PrintAddress3
  1251.  
  1252.  ." ComputerLand"
  1253.  ;
  1254.  
  1255. So, we have now created all the elements of our simple program, so let us
  1256. collect them all together:
  1257.  
  1258.  \ ****************
  1259.  \ Start of program
  1260.  \ ****************
  1261.  
  1262.  :  ClearScreen
  1263.  
  1264.  SCRCLR
  1265.  ;
  1266.  
  1267.  : SetNamePosition
  1268.  
  1269.  CURPUT
  1270.  CURSAVE
  1271.  ;
  1272.  
  1273.  : SetNameTextStyle
  1274.  
  1275.  1 FPENSET
  1276.  BOLDON
  1277.  ;
  1278.  
  1279.  : PrintName
  1280.  
  1281.  ." Mr. W. Smith"
  1282.  ;
  1283.  
  1284.  : NextScreenLine
  1285.  
  1286.  CURSET
  1287.  1 CURDN
  1288.  CURSAVE
  1289.  ;
  1290.  
  1291.  : SetAddTextStyle
  1292.  
  1293.  BOLDOFF
  1294.  ITALON
  1295.  ;
  1296.  
  1297.  : PrintAddress1
  1298.  
  1299.  ." 12, Blitter Street"
  1300.  ;
  1301.  
  1302.  : PrintAddress2
  1303.  
  1304.  ." AmigaTown"
  1305.  ;
  1306.  
  1307.  : PrintAddress3
  1308.  
  1309.  ." ComputerLand"
  1310.  ;
  1311.  
  1312.  : PrintAddress
  1313.  
  1314.    ClearScreen
  1315.    SetNamePosition
  1316.    SetNameTextStyle
  1317.    PrintName
  1318.    NextScreenLine
  1319.    SetAddTextStyle
  1320.    PrintAddress1
  1321.    NextScreenLine
  1322.    PrintAddress2
  1323.    NextScreenLine
  1324.    PrintAddress3
  1325.  ;
  1326.  
  1327.  \ **************
  1328.  \ End of program
  1329.  \ **************
  1330.  
  1331. Try compiling the small program above, either by:
  1332.  
  1333. 1. Highlighting it and pressing Amiga-e
  1334. 2. Cutting and pasting it into another editor, and running it from there.
  1335. 3. Typing it in at the command line or another editor.
  1336.  
  1337. Once you have compiled this code you can do a USER-vocabulary listing and 
  1338. you will see each one of your new commands listed.
  1339.  
  1340. To see your new program working, type at the command line:
  1341.  
  1342. 12 12 PrintAddress WAITSPACE
  1343. ^^ ^^
  1344. Cursor position parameters.
  1345.  
  1346. Note that we have supplied two parameters to PrintAddress, whose stack
  1347. diagram is as follows:
  1348.  
  1349.   PrintAddress    ( Column, Row - - - )
  1350.  
  1351.  
  1352. This is an interesting point, because you will remember that we actually
  1353. needed these two parameters originally to satisfy the requirements of the
  1354. command word CURPUT.
  1355.  
  1356. Then we included CURPUT in "SetNamePosition", so we realised that the same
  1357. two parameters would need to be supplied for this word, so that they could
  1358. in turn be passed to CURPUT.
  1359.  
  1360. Actually "SetNamePosition" is the SECOND command in our program sequence,
  1361. with "ClearScreen" coming first.  However, no matter where in the program
  1362. SetNamePosition comes, it still needs its two parameters supplied on the
  1363. stack.  Once these parameters have been placed on the stack, provided that
  1364. nothing changes the stack in the interim, any number of commands can be
  1365. executed before SetNamePosition eventually receives the stacked parameters.
  1366.  
  1367. So what happens to the two parameters for PrintAddress?
  1368.  
  1369. Think of it this way: when CURPUT, which is a sub-part of our program,
  1370. eventually executes, it will need two parameters, which it will pull out
  1371. of the general storage area we call the stack.  As far as CURPUT is
  1372. concerned, it does not care when or how the parameters got there, and in
  1373. fact it will fetch the last two numbers stored on the stack no matter
  1374. whether they are the correct ones or not.
  1375.  
  1376. In our example the two CURPUT parameters are placed on the stack before the
  1377. main PrintAddress command is executed, and they remain unused and unaltered
  1378. on the stack until CURPUT is finally executed within the second sub-command
  1379. SetNamePosition.
  1380.  
  1381. It is YOUR job as a programmer to make sure that at the time when CURPUT
  1382. executes the last (top) two numbers on the stack are the required cursor
  1383. position parameters.
  1384.  
  1385. You can place these parameters on the HeliOS stack at any time: in fact
  1386. you can choose whether to type them in when you run the program, as above,
  1387. or include them in the program.
  1388.  
  1389. You COULD do any of these:
  1390.  
  1391.  
  1392.  : PrintAddress           : PrintAddress           : PrintAddress
  1393.  
  1394.    12 12                    ClearScreen              ClearScreen
  1395.    ClearScreen              12 12                    SetNamePosition
  1396.    SetNamePosition          SetNamePosition          12 12
  1397.    SetNameTextStyle         SetNameTextStyle         SetNameTextStyle
  1398.    PrintName                PrintName                PrintName
  1399.    NextScreenLine           NextScreenLine           NextScreenLine
  1400.    SetAddTextStyle          SetAddTextStyle          SetAddTextStyle
  1401.    PrintAddress1            PrintAddress1            PrintAddress1
  1402.    NextScreenLine           NextScreenLine           NextScreenLine
  1403.    PrintAddress2            PrintAddress2            PrintAddress2
  1404.    NextScreenLine           NextScreenLine           NextScreenLine
  1405.    PrintAddress3            PrintAddress3            PrintAddress3
  1406.  ;                        ;                        ;
  1407.  
  1408. Can you see why the first two examples are OK but the third one will fail?
  1409.  
  1410. Yes...the parameters are NOT on the stack when SetNamePosition executes,
  1411. but are placed there afterwards, which is too late.
  1412.  
  1413. So, here we have another very important point to remember:
  1414.  
  1415. * IT IS THE PROGRAMMER'S RESPONSIBILITY ALWAYS TO ENSURE THAT CORRECT
  1416.   PARAMETERS ARE PRESENT ON THE STACK AT ALL TIMES
  1417.  
  1418. This is called "stack handling", and is an absolutely vital part of your
  1419. initial learning of HeliOS.
  1420.  
  1421. HeliOS uses a stack because this is an extremely fast and efficient way of
  1422. passing parameters between commands.  You can actually use named variables,
  1423. or named constants, as you will soon see, but the stack is fastest and in
  1424. many ways easiest once you are used to it.
  1425.  
  1426. We will discuss the stack in depth later, but remember for now that a good
  1427. HeliOS programmer should always keep a clear idea of just what is "on the
  1428. stack" at any part of a program.
  1429.  
  1430. You can easily write yourself stack comments within your source code to help
  1431. you with this if you like:
  1432.  
  1433.  
  1434. : PrintAddress           \ Stack = Empty
  1435.  
  1436.   12 12                  \ Stack = 12 12
  1437.   ClearScreen            \ Stack = 12 12
  1438.   SetNamePosition        \ Stack = Empty
  1439.   SetNameTextStyle
  1440.   PrintName
  1441.   NextScreenLine
  1442.   SetAddTextStyle
  1443.   PrintAddress1
  1444.   NextScreenLine
  1445.   PrintAddress2
  1446.   NextScreenLine
  1447.   PrintAddress3
  1448. ;
  1449.  
  1450. You can also write special lines within your code which halt the program, 
  1451. display the current stack parameters (without changing them) and wait for
  1452. you to press <Space> before continuing.  In this way you can easily step
  1453. through a program sequentially, keeping an eye on the stack as you go to
  1454. ensure that everything is working how you expected.
  1455.  
  1456. This is a really useful way if debugging a program which is misbehaving,
  1457. and you should become familiar with the technique right from the start of
  1458. your HeliOS programming.
  1459.  
  1460. Here is the sort of line which you might insert into your code to give you
  1461. a useful debug/stack report:
  1462.  
  1463. CR ." Arrived at Position 1, and stack = " .S WAITSPACE CR
  1464.  
  1465. Notice the use of "CR" (carriage return) to separate the text lines.
  1466.  
  1467. If you were debugging the program above, and you wished to see what was
  1468. happening as you stepped through it sequentially, you might do something
  1469. like this:
  1470.  
  1471. : PrintAddress
  1472.  
  1473.   CR ." Arrived at Position 1, and stack = " .S WAITSPACE CR
  1474.   ClearScreen
  1475.   CR ." Arrived at Position 2, and stack = " .S WAITSPACE CR
  1476.   SetNamePosition
  1477.   CR ." Arrived at Position 3, and stack = " .S WAITSPACE CR
  1478.   SetNameTextStyle
  1479.   CR ." Arrived at Position 4, and stack = " .S WAITSPACE CR
  1480.   PrintName
  1481.   CR ." Arrived at Position 5, and stack = " .S WAITSPACE CR
  1482.   NextScreenLine
  1483.   CR ." Arrived at Position 6, and stack = " .S WAITSPACE CR
  1484.   SetAddTextStyle
  1485.   CR ." Arrived at Position 7, and stack = " .S WAITSPACE CR
  1486.   PrintAddress1
  1487.   CR ." Arrived at Position 8, and stack = " .S WAITSPACE CR
  1488.   NextScreenLine
  1489.   CR ." Arrived at Position 9, and stack = " .S WAITSPACE CR
  1490.   PrintAddress2
  1491.   CR ." Arrived at Position 10, and stack = " .S WAITSPACE CR
  1492.   NextScreenLine
  1493.   CR ." Arrived at Position 11, and stack = " .S WAITSPACE CR
  1494.   PrintAddress3
  1495.   CR ." Arrived at Position 12, and stack = " .S WAITSPACE CR
  1496. ;
  1497.  
  1498. Notice here that we could simplify the code by creating a definition:
  1499.  
  1500. : Debug CR ." Arrived at Position " . .", and stack = " .S WAITSPACE CR ;
  1501.  
  1502. Then we could use it like this:
  1503.  
  1504. 1 Debug
  1505. 2 Debug
  1506. 3 Debug
  1507. etc. etc.
  1508.  
  1509. Can you see how this works?
  1510.  
  1511. Of course, in this example, the program itself is outputting text to the
  1512. screen, and the debug stack information will be mixed in with the program
  1513. text output.  This is messy, but it does not really matter about the mixed
  1514. screen output provided that you get the information you require.  Notice
  1515. that we included "CR" commands at the start and end of each debug line so
  1516. that the debug text could more easily be separated from the program output.
  1517.  
  1518. Of course, in a real debugging situation you would probably not have debug
  1519. lines for EVERY sub-command, and you could organise your text output by
  1520. sending debug text to a different stream if you wished.  This sort of thing
  1521. is down to personal style, and the important thing is that you know how to
  1522. use this technique as a tool for debugging when required.  Often you only
  1523. need to do this sort of thing for quick "rough and ready" checking, and it
  1524. is usually not worth bothering about setting up special text streams etc..
  1525.  
  1526. Let us now go back and look again at our original command line, where we
  1527. typed in the parameters before we ran the program:
  1528.  
  1529. 12 12 PrintAddress WAITSPACE
  1530.  
  1531. Notice here that the useful command "WAITSPACE" is being used once again.
  1532.  
  1533. The "WAITSPACE" will allow us to observe the results of the program without
  1534. anything else printing to the screen until <Space> is pressed: in this case
  1535. we shall be able to observe our address printout without the HeliOS "end of
  1536. session" text disturbing the display until we press <Space>.
  1537.  
  1538. This is often quite useful, and you will probably find it convenient on many
  1539. occasions to use the WAITSPACE command to halt a program temporarily.
  1540.  
  1541. ------------------------------------
  1542. Order of creation of HeliOS commands
  1543. ------------------------------------
  1544.  
  1545. Notice that when we collected together all our sub-commands into the small
  1546. program we constructed our main command "PrintAddress" last, AFTER we had
  1547. made all our sub-commands.
  1548.  
  1549. If you think about it, this is obviously necessary, because you cannot
  1550. instruct HeliOS to use any command which you have not yet taught it!
  1551.  
  1552. In general, when writing HeliOS programs, you must always progress from
  1553. simple words using HeliOS CORE functions to more complex compound words
  1554. which can use commands you have already created earlier.
  1555.  
  1556. There are various ways to avoid this strict necessity for sequential command
  1557. creation (usually via what is called "vectored execution"), but don't worry
  1558. about this until a later more advanced tutorial.....
  1559.  
  1560. So this is another important general rule:
  1561.  
  1562. * NEW HELIOS COMMANDS CAN ONLY CALL DIRECTLY EITHER:
  1563.  
  1564.   1. HELIOS CORE COMMANDS
  1565.  
  1566.   OR
  1567.  
  1568.   2. COMMAND WORDS YOU HAVE DEFINED EARLIER IN THE PROGRAM
  1569.  
  1570. ***********************************************************************
  1571.  
  1572. --------------------------
  1573. Organisation and structure
  1574. --------------------------
  1575.  
  1576. In the example above we made a small attempt to improve the organisation
  1577. of our program between the stages of planning and final implementation.
  1578.  
  1579. Although the processing of information in a computer is essentially
  1580. sequential in nature, and your early programs will be simple sequences
  1581. of commands, it should soon become apparent that some degree of planning
  1582. and organisation is necessary to design an efficient program.
  1583.  
  1584. The fact is that computer programs contain structure, with small parts
  1585. of the whole working together in sometimes complex ways.  Once you begin
  1586. to write programs which can behave differently with different inputs, for
  1587. example, you can start to generate quite a high level of complexity.
  1588.  
  1589. There is never "only one way" to perform any complex task, and you will
  1590. often be faced with many ways of implementing a certain programming
  1591. requirement.
  1592.  
  1593. Always look carefully at the overall "structure" of your programs, and
  1594. at the way you have broken complex operations into smaller tasks.  This
  1595. is very important, and good organization goes together with sensible
  1596. and logical structuring of commands.  Of course this is a very personal
  1597. thing, and what is sensible to one person is idiotic to another!  In
  1598. spite of this, there IS often an overall optimum strategy which may be
  1599. determined by the constraints of the way the computer works.
  1600.  
  1601. In general you should try to balance economy of code with speed and
  1602. simplicity of operation: there will always be constraints and tradeoffs,
  1603. but you should always be in sufficient control to strike a good balance.
  1604.  
  1605. You should always PLAN your programs in such a way that there is minimal
  1606. duplication of effort, and you should always try to keep a view of the
  1607. greater whole as well as the individual parts.
  1608.  
  1609. In HeliOS programs you will:
  1610.  
  1611. 1. Break down large tasks into smaller sections.
  1612.  
  1613. 2. Choose appropriate and sensible names for these sub-commands in
  1614.    order to make your programs comprehensible and easily readable.
  1615.  
  1616. 3. Break down your sub-commands into still smaller and more manageable
  1617.    sections until you reach the lowest level of simple CORE commands.
  1618.  
  1619. Simple sequential programs are fairly straighforward in terms of logical
  1620. design, but you will encounter circumstances where your program has to
  1621. behave differently according to external or internal conditions.  The way
  1622. sequential computer operations can be made to "adapt" with respect to time
  1623. and different circumstances is responsible for much of the fascination of
  1624. computer programming.
  1625.  
  1626. Computers would not be capable of nearly so many useful functions if they
  1627. could only carry out sequences of preprogrammed tasks.  In fact computers
  1628. are ingenious in that they can take "decisions" and modify their behaviour
  1629. depending upon the results of their own actions.
  1630.  
  1631. This "intelligent" behaviour comes, amazingly, from a simple use of
  1632. logical operations such as:
  1633.  
  1634. * Waiting for a certain condition to be "true".
  1635.  
  1636. * Doing one of several different things depending on a condition.
  1637.  
  1638. * Repeating "similar" or "modified" actions depending upon conditions.
  1639.  
  1640. * Counting and performing multiple tasks.
  1641.  
  1642. * etc. etc.
  1643.  
  1644. These considerations of "contingency" allow even the simplest of computer
  1645. programs to have non-linear structures, and it is here that flexiblity and
  1646. intelligence on the part of the programmer come to the fore.
  1647.  
  1648. So, we have two forms of "structure" to consider in our early programming
  1649. efforts:
  1650.  
  1651. 1. Structure in terms of the breakdown of tasks into manageable "chunks",
  1652.    which really means structure in terms of code modularity and function.
  1653.  
  1654. 2. Structure in terms of logical behaviour, response to external or
  1655.    internal conditions, time related constraints etc.
  1656.  
  1657. -----------------------------------------------------------
  1658. Using IF...ELSE...THEN constructs in conditional statements
  1659. -----------------------------------------------------------
  1660.  
  1661. Here is a simple HeliOS conditional construct, showing how a program can
  1662. easily interact with the outside world:
  1663.  
  1664.  : PressSpace?
  1665.  
  1666.  ." Please press a key."           \ Ask for user input
  1667.  
  1668.  CR                                \ CR = Carriage Return
  1669.  
  1670.  KEY                               \ Wait for a keypress
  1671.  
  1672.  32                                \ 32 = Code for <Space>
  1673.  =                                 \ Key = <Space>?
  1674.  IF
  1675.    ." Space was pressed!"          \ Yes!
  1676.  ELSE
  1677.    ." Space was not pressed!"      \ No!
  1678.  THEN
  1679.  
  1680.  CR                                \ CR = Carriage Return
  1681.  
  1682.  ." Thank you!"                    \ Finished!
  1683.  ;
  1684.  
  1685. Here you can see that one of two different logical paths will be taken
  1686. by the program depending on user input.
  1687.  
  1688. Look how HeliOS implemented the "conditional" construct of IF...ELSE...
  1689. THEN.
  1690.  
  1691. Here is what happens in the above program:
  1692.  
  1693. 1. We print a message and then do a "carriage return".  This is easy.
  1694.  
  1695. 2. We use the HeliOS CORE command "KEY", which waits for user input and
  1696.    returns a numerical value depending upon which key was pressed.
  1697.  
  1698.    The stack diagram of KEY would be  ( - - - KeyValue )
  1699.  
  1700.    This means that after KEY there is one number, an input code, on
  1701.    the stack.
  1702.  
  1703. 3. The next entry in our program is "32", which puts the number 32 (the
  1704.    key code for <Space>) onto the parameter storage stack, on top of the
  1705.    input code.
  1706.  
  1707.    We now have two numbers on the HeliOS stack: ( KeyValue, 32 - - - ).
  1708.  
  1709. 4. The command "=" is used to compare two numbers which are fetched from
  1710.    the stack.
  1711.  
  1712.    As "=" gets the numbers from the stack it removes them, leaving the
  1713.    stack empty.
  1714.  
  1715.    When "=" has compared the two numbers, it will place a result flag onto
  1716.    the stack.
  1717.  
  1718.    A "1" will be left on the stack if the two numbers were equal, and a
  1719.    "0" will be returned if they were unequal.
  1720.  
  1721.    The stack diagram of "=" is     ( number1 number2 - - - Flag )
  1722.                                                            0 or 1
  1723.  
  1724.    or, in our example:             ( KeyValue, 32 - - -  Flag)
  1725.  
  1726. 5. Now comes the important part: the use of the conditional "IF" command.
  1727.  
  1728.    The command "IF" removes a number (the flag) from the stack and does one
  1729.    of two different things according to whether the flag is zero or non-zero.
  1730.  
  1731.    If the flag value is non-zero, the code immediately after "IF" will be
  1732.    executed up to the point where either "ELSE" or "THEN" occur.
  1733.  
  1734.    If the number is zero, the code immediately after "IF" will be skipped
  1735.    and execution of code will continue immediately after the first occurence
  1736.    of "ELSE" or "THEN".
  1737.  
  1738. 6. The "ELSE" and "THEN" determine the code which will be executed after
  1739.    "IF", depending on conditions following the "=" comparison.
  1740.  
  1741. 7. The program completes by ALWAYS executing all the code after "THEN", in
  1742.    this case with a simple exit message.
  1743.  
  1744.  
  1745. Here is another way to use IF and THEN.
  1746.  
  1747.  : PressSpace?
  1748.  
  1749.  ." Please press a key."           \ Ask for user input
  1750.  
  1751.  CR                                \ CR = Carriage Return
  1752.  
  1753.  KEY                               \ Wait for a keypress
  1754.  
  1755.  32                                \ 32 = Code for <Space>
  1756.  =                                 \ Key = <Space>?
  1757.  IF
  1758.    ." Space was pressed!"          \ Yes!
  1759.  THEN
  1760.  
  1761.  CR                                \ CR = Carriage Return
  1762.  
  1763.  ." Thank you!"                    \ Finished!
  1764.  ;
  1765.  
  1766. This was actually simpler!
  1767.  
  1768. We simply used "IF" to determine whether or not to print our message, and 
  1769. we have no alternative action defined by the "ELSE" command.
  1770.  
  1771. Look at these simple conditional constructs and make sure that you really
  1772. understand what is happening.
  1773.  
  1774. In particular, try to follow exactly what is happening to numbers on the
  1775. stack.
  1776.  
  1777. Here is the first program again with stack comments:
  1778.  
  1779.  : PressSpace?
  1780.  
  1781.  ." Please press a key."         \ Ask for user input      Stack = Empty
  1782.  
  1783.  CR                              \ CR = Carriage Return    Stack = Empty
  1784.  
  1785.  KEY                             \ Wait for a keypress     Stack = Input
  1786.  
  1787.  32                              \ 32 = Code for <Space>   Stack = Input, 32
  1788.  =                               \ Key = <Space>?          Stack = Result
  1789.  IF                              \                         Stack = Empty
  1790.    ." Space was pressed!"        \ Yes!                        etc.
  1791.  ELSE
  1792.    ." Space was not pressed!"    \ No!
  1793.  THEN
  1794.  
  1795.  CR                              \ CR = Carriage Return
  1796.  
  1797.  ." Thank you!"                  \ Finished!
  1798.  ;
  1799.  
  1800.  
  1801. Important Notes:
  1802.  
  1803. * Always remember that IF...ELSE...THEN constructs must always be contained
  1804.   within colon definitions: you cannot use these kinds of logical constructs
  1805.   in code which is being directly interpreted.
  1806.  
  1807. * IF...ELSE...THEN constructs must be wholly contained within discrete word
  1808.   definitions: you cannot implement an IF...THEN...ELSE construct between
  1809.   different colon definitions.
  1810.  
  1811. * IF...ELSE...THEN constructs can be freely "nested" within each other, to
  1812.   any level you wish.
  1813.  
  1814. Now you know how to build very complex conditional statements if required,
  1815. but beware of making these constructions too complex.  If your code starts
  1816. to become top-heavy with nested conditional statements, you can generally
  1817. be sure that there is a better (more efficient and easier to read and debug)
  1818. way of doing things.  Often it is easier and faster to use such things as
  1819. vectored execution (see later) rather than having a whole series of hard to
  1820. follow in-line conditional statements.
  1821.  
  1822. ---------------
  1823. Combining flags
  1824. ---------------
  1825.  
  1826. There is another useful set of techniques which might help you simplify your
  1827. conditional constructions, and these involve the various ways of combining
  1828. "truth" flags.
  1829.  
  1830. If you have several different flags to test it can often be convenient to
  1831. use the "OR" function to combine them.  Since "IF" is only interested in
  1832. whether a flag is zero or non-zero, it is easy to combine any number of
  1833. separate flags into one value by simply using a bitwise OR.
  1834.  
  1835. You can also combine flags using logical AND and XOR operations, and in
  1836. many cases these techniques are very useful, but be careful in doing this
  1837. that you are operating on flags with compatible bit structures.  This means
  1838. that while it is OK to do an AND test on two flags whose values MUST be
  1839. either "1" or "0", you would cause an error if you tried to combine two
  1840. flags which could take different non-zero values.
  1841.  
  1842. For example:
  1843.  
  1844. 1 1 AND = 1 -> OK
  1845. 0 1 AND = 0 -> OK
  1846. 1 0 AND = 1 -> OK
  1847.  
  1848. 1 2 AND = 0 -> NOT OK  (Both flags are non-zero, or "true", but the
  1849.                         combination of both with AND is false)
  1850.  
  1851. Always remember in these cases that you are dealing with bitwise operations.
  1852.  
  1853.  
  1854. ----------------------------------
  1855. Functions, Data and Error Checking
  1856. ----------------------------------
  1857.  
  1858. Another way of analysing a program is in terms of the way "functions" are
  1859. used to operate on "data": whether a command "DOES" something (like the
  1860. addition function) or simply "IS" something (like a number which is added).
  1861.  
  1862. The syntax of some languages strictly controls how these different
  1863. categories are represented, to help you ensure that you write your code
  1864. in a way which prevents "incorrect" expressions and mixed parameter use.
  1865.  
  1866. HeliOS has no such enforced constraint, although you can, if you wish,
  1867. invent naming conventions for yourself by using certain prefixes for
  1868. certain types of function etc..
  1869.  
  1870. Yet another abstraction is to specify the TYPE of "data/something" which
  1871. you are dealing with.  In some languages there is a very strict limitation
  1872. concerning which types of data each kind of function may operate upon.
  1873.  
  1874. This kind of "artificial" abstraction can have its uses, especially in
  1875. helping the computer language to "enforce" so-called "correct" programming
  1876. techniques.  However, checking all these things takes valuable computer
  1877. time and also enforces a rigid "straightjacket" on the programmer.
  1878.  
  1879. The fact is that all these "human" intellectual abstractions are basically
  1880. an imposition upon the way the computer works, since to a computer ALL
  1881. data is simply numerical and all processes obey a very simple binary logic.
  1882.  
  1883. Not only this, but restrictive rules built into a computer language can
  1884. also soon become very irksome to an experienced programmer who is accustomed
  1885. to greater freedom.
  1886.  
  1887. HeliOS keeps things simple and efficient, staying close to the way the
  1888. computer CPU works and avoiding too many artificial constraints.
  1889.  
  1890. HeliOS, therefore, has no enforcement of such things as "data types", and
  1891. does very little by way of enforcement or error checking (Although since
  1892. HeliOS is extensible and user-definable you could build such a system for
  1893. yourself if you wished to do so).
  1894.  
  1895. HeliOS provides a comprehensive and powerful set of logical tools such
  1896. as looping, timing, and conditional constructs.  It provides all the usual
  1897. data constructs such as constants, variables, text strings, data structures
  1898. and pointers.  It then leaves you free to evolve your own methods.......
  1899.  
  1900. HeliOS does, however, enforce "structured programming" in that it will not
  1901. easily allow you to jump from place to place forward and backward in your
  1902. code.  This is a good thing, because if allowed to do so most beginners
  1903. tend to write a tangled web of "spaghetti-logic" code and then often give
  1904. up before they have managed to disentangle themselves!
  1905.  
  1906. In general HeliOS programs progress logically and sequentially using simple
  1907. logical constructs such as IF....ELSE....THEN or BEGIN....UNTIL.  HeliOS
  1908. keeps things simple, free of constraint, and powerful.  It us down to you,
  1909. the programmer, to make sure that you use this freedom wisely, because
  1910. HeliOS will not waste time checking for your mistakes or forcing you to do
  1911. things "right".
  1912.  
  1913. ------------------------------------------------------------------
  1914. The HeliOS vocabulary, the dictionary, and the compilation process
  1915. ------------------------------------------------------------------
  1916.  
  1917. Every command word which you create immediately becomes a fully fledged
  1918. part of the HeliOS command set, and can be used exactly like a HeliOS CORE
  1919. function.  The name of the command is immediately entered in the HeliOS
  1920. vocabulary, as can be verified by performing a vocabulary listing.
  1921.  
  1922. When you create a new command, that command is instantly "compiled", which
  1923. involves two things:
  1924.  
  1925. 1. HeliOS creates a special section of code which will be executed when
  1926.    your new command is called.
  1927.  
  1928. 2. HeliOS creates a new vocabulary entry.
  1929.  
  1930. These two actions, when repeated, build up two sections of information
  1931. in the computer's memory, and these two sections of information can be
  1932. saved and reloaded.
  1933.  
  1934. HeliOS saves all functional code in one memory block and all vocabulary
  1935. "name" information in another separate memory block.
  1936.  
  1937. As you will see later, you can actually dispense with the "vocabulary"
  1938. data when "running" a stand-alone HeliOS program.  The vocabulary data
  1939. is really a simple reference listing of command words which is only
  1940. required when you are initially compiling a program or using the HeliOS
  1941. interpreter.
  1942.  
  1943. Here are two important statements which you should try to understand
  1944. and bear in mind as you work with HeliOS:
  1945.  
  1946. 1.  INTERPRETING:  When HeliOS "interprets" a command word the function
  1947.                    associated with that word is actually performed, and
  1948.                    nothing is aded to the vocabulary.
  1949.  
  1950. 2.  COMPILING:     When you ask HeliOS to "compile" a new command word
  1951.                    using a colon definition this is what happens:
  1952.  
  1953.                    a. The new command name is added to the vocabulary
  1954.  
  1955.                    b. The command functions whose names comprise the "body"
  1956.                       or "program" of the new word will NOT (usually) be
  1957.                       performed directly, but will be stored as the code
  1958.                       part of the new word.
  1959.  
  1960.                    c. The newly compiled code which defines the new command
  1961.                       function will not be executed until later, when the
  1962.                       new command name is eventually used in INTERPRET mode.
  1963.  
  1964.  
  1965. Here are two examples:
  1966.  
  1967. 1.
  1968.  
  1969.   ." Hello World!"
  1970.  
  1971.   - Prints "Hello World!" and does not compile anything.
  1972.  
  1973.   This is an example of INTERPRET mode.
  1974.  
  1975. 2.
  1976.  
  1977.   : HELLO_WORLD  ." Hello World!" ;
  1978.  
  1979.   - Prints nothing but compiles a command called "HELLO_WORLD" which
  1980.     WHEN EXECUTED OR INTERPRETED LATER will print "Hello World!".
  1981.  
  1982.   This is an example of COMPILE mode.
  1983.  
  1984. Note that you can always examine the current HeliOS vocabulary by using
  1985. the Interpreter menu functions or the command line functions:
  1986.  
  1987. VLIST  - Display CORE and USER vocabulary.
  1988. UVLIST - Display USER vocabulary.
  1989. CVLIST - Display CORE vocabulary.
  1990.  
  1991. So far we have referred generally to the set of HeliOS available command
  1992. words as the VOCABULARY, and to the documentation describing and defining
  1993. these commands as the DICTIONARY.
  1994.  
  1995. This is correct, but in FORTH the term DICTIONARY is also traditionally
  1996. used to refer to the actual memory area where all commands are stored.
  1997.  
  1998. In traditional FORTH the dictionary memory was used to store both command
  1999. names and code, but HeliOS uses a rather different internal arrangement
  2000. with the command names stored separately.
  2001.  
  2002. HeliOS documentation and usage adheres to FORTH conventions by continuing
  2003. to use the term DICTIONARY loosely to refer to the memory area where the
  2004. code for compiled words is stored.  You will see a readout of the current
  2005. available dictionary space at the top left of the HeliOS status display.
  2006.  
  2007. Once a command word is in the vocabulary, HeliOS will always understand
  2008. this word if you type it in your programs or at the command line.  In
  2009. fact, just like a person, HeliOS "understands" your instructions by
  2010. interpreting words, like this:
  2011.  
  2012. 1. HeliOS scans everything you write, one space-delimited word at a time.
  2013.  
  2014. 2. It looks up each word in its vocabulary.
  2015.  
  2016. 3. If it finds the word in its vocabulary it carries out the associated
  2017.    command function.
  2018.  
  2019. 4. If it does not find the word in its vocabulary, HeliOS will try to
  2020.    find a matching symbol in its include files, and if it does so the
  2021.    numeric value for that include symbol will be substituted for the
  2022.    symbolic name.
  2023.  
  2024. 5. If HeliOS cannot find the word in either its vocabulary or include symbol
  2025.    lists, it tries to interpret the word as a pure number.
  2026.  
  2027. 6. If all these options fail HeliOS issues a warning to you and stops
  2028.    the process of interpreting immediately.
  2029.  
  2030. To summarize, here are simple definitions of the "technical" terms used
  2031. when referring to program compilation/interpretation:
  2032.  
  2033. EXECUTION      = HeliOS takes a given word and carries out the
  2034.                  associated code.
  2035.  
  2036. INTERPRETATION = HeliOS reads your input and acts upon it by
  2037.                  executing command words sequentially.
  2038.  
  2039. COMPILATION    = HeliOS creates a new command and stores the name
  2040.                  and the associated functional code.
  2041.  
  2042. In case you are interested in more details, here is a brief description
  2043. of how HeliOS carries out compilation of a colon definition:
  2044.  
  2045. * HeliOS reads the input text and finds the colon character.
  2046.  
  2047. * It then EXECUTES the code associated with the COLON command word.
  2048.  
  2049. * The COLON command code, when EXECUTED, does several things:
  2050.  
  2051.      (To start with HeliOS is in "interpret mode".)
  2052.  
  2053.   1. It COMPILES the following word, the command name, into the vocabulary.
  2054.  
  2055.   2. It sets up an internal operational MODE such that HeliOS will now
  2056.      COMPILE all following words into the dictionary, until a SEMICOLON
  2057.      is encountered.
  2058.  
  2059.      (HeliOS is now in "compile mode".)
  2060.  
  2061.   3. It continues to COMPILE each successive word into the body of the new
  2062.      word.
  2063.  
  2064.   4. It finds a SEMICOLON and terminates the new word definition,
  2065.      reverting back to INTERPRET mode.
  2066.  
  2067.      (HeliOS is now back in "interpret mode".)
  2068.  
  2069. -----------------------------------------
  2070. The concepts of RUN-TIME and COMPILE-TIME
  2071. -----------------------------------------
  2072.  
  2073. It will be more apparent to you what these terms mean when you have used
  2074. HeliOS for some time, and we will cover these matters is more detail later,
  2075. but even at this early stage we should give a brief mention to these two
  2076. important concepts.
  2077.  
  2078. For any command word:
  2079.  
  2080. COMPILE-TIME is the time when the word is compiled
  2081.  
  2082. RUN-TIME     is the time when the word is executed, either when a program
  2083.              executes or when a word is interpeted at the command line.
  2084.  
  2085. This sounds simple and obvious but there are subtleties to the situation!
  2086.  
  2087. For example, when a new word is being created, it is "RUN-TIME" for the
  2088. HeliOS command words which actually DO THE COMPILING, but "COMPILE-TIME"
  2089. for the new word itself.
  2090.  
  2091. In other words, the terms RUN_TIME and COMPILE-TIME relate to states of
  2092. particular words at a given time, and NOT the HeliOS system as a whole.
  2093.  
  2094.  
  2095. -----------------------------------
  2096. Creating standalone HeliOS programs
  2097. -----------------------------------
  2098.  
  2099. By the term "standalone" we refer to the situation where you want to use
  2100. or distribute a HeliOS program without the main HeliOS Interpreter.
  2101.  
  2102. This topic is discussed in other parts of the HeliOS documentation, but
  2103. here we will give you a simple tutorial introduction.
  2104.  
  2105. Before discussing the generation of standalone programs we should perhaps
  2106. mention again here that HeliOS has an "autorun" feature in which the last
  2107. word in a program will automatically be run when the code is re-executed
  2108. in a standalone environment.  Thus you should always set up the last colon
  2109. definition of a standalone program as the startup code for the program.
  2110.  
  2111. HeliOS has two methods of generating standalone programs, and each has
  2112. advantages and disadvantages:
  2113.  
  2114.  
  2115. 1. Generating a (small in size) overlay program file for use with the
  2116.    separate HeliOS_exe executive program.
  2117.  
  2118.    This method has the advantage that you can put several HeliOS
  2119.    programs on one disk with just the one large executive file which
  2120.    can be used by all of the individual programs.
  2121.  
  2122.    This method has the disadvantage that you need two files to run
  2123.    each of your programs, using an AmigaDOS command such as:
  2124.  
  2125.    HeliOS_exe MyProgram
  2126.  
  2127.    Note that the HeliOS executive is typically 250k in size, but when
  2128.    packed this reduces to around 100k.
  2129.  
  2130.  
  2131. 2. Generating a single standalone executable HeliOS program.
  2132.  
  2133.    This method has the advantage that you have just one file.
  2134.  
  2135.    This method has the disadvantage that each HeliOS standalone
  2136.    executable has to contain all the HeliOS support code.  This
  2137.    gives a large file size for each HeliOS program, and even the
  2138.    most tiny program has the same executable overhead.
  2139.  
  2140.    The HeliOS executable is typically 250k in size, but when packed
  2141.    this reduces to around 100k.
  2142.  
  2143. We will now look at methods of generating the various kinds of HeliOS
  2144. program code, concentrating on the use of the PRESAVE function which
  2145. is found in the Interpreter menus.
  2146.  
  2147. If you examine the Interpreter menus you will see that there are menu
  2148. options to configure the PRESAVE output compilation process into three
  2149. distinct modes:
  2150.  
  2151. 1 -> Compile dictionary
  2152.  
  2153. 2 -> Compile overlay
  2154.  
  2155. 3 -> Compile standalone
  2156.  
  2157. These options cause the PRESAVE function to save different kinds of
  2158. program files as follows:
  2159.  
  2160. Compile dictionary - This causes TWO files to be generated and saved by
  2161.                      the PRESAVE function.
  2162.  
  2163.                      These files are a main program overlay file and a
  2164.                      vocabulary overlay file for use when the program
  2165.                      is to be PRELOADED back into the main Interpreter.
  2166.  
  2167.                      In this process the vital information which makes
  2168.                      up a HeliOS program is split into two parts, one
  2169.                      containing the program code itself, and the other
  2170.                      containing "vocabulary" information required for
  2171.                      the Interpreter to be able to associate keyboard
  2172.                      input with functions defined within the program.
  2173.  
  2174.                      The main program file is given a ".cmp" suffix,
  2175.                      and the vocabulary file a ".cmpv" suffix.  Both
  2176.                      these files should be kept together, and both will
  2177.                      be automatically loaded when you use the PRELOAD
  2178.                      function.
  2179.  
  2180.                      Note that when using the PRELOAD function you only
  2181.                      need to select the ".cmp" file, because the ".cmpv"
  2182.                      file will be automatically loaded at the same time
  2183.                      by the HeliOS system.
  2184.  
  2185. Compile overlay    - This causes a single file to be generated for use
  2186.                      with the "HeliOS_exe" standalone executive program:
  2187.                      this file simply contains the program code in a
  2188.                      concisely packed form.
  2189.  
  2190.                      The standalone executive does not make any use of
  2191.                      vocabulary information, so it only requires the
  2192.                      program code overlay.
  2193.  
  2194. Compile standalone - This causes a single executable file to be generated,
  2195.                      which can be run directly on its own.
  2196.  
  2197.  
  2198. Note here that a standalone single executable HeliOS program can ONLY be
  2199. generated from the main Interpreter, but the overlay files can also be
  2200. created by using the standalone compiler "HeliOS_cmp".
  2201.  
  2202. We will now give a few direct practical instructions on how to generate
  2203. standalone HeliOS programs.
  2204.  
  2205.  
  2206. * To save a HeliOS standalone overlay
  2207.  
  2208.   1. Load and compile your program, remembering that the last word in the
  2209.      program will automatically be run when your code is re-executed in a
  2210.      standalone environment.
  2211.  
  2212.   2. Select the "Compile overlay" Interpreter menu option.
  2213.  
  2214.   3. Select the "Presave" Interpreter menu option to save the overlay.
  2215.  
  2216.   4. Execute your program using the AmigaDOS command line:
  2217.  
  2218.      HeliOS_exe MyProgramOverlay
  2219.  
  2220.  
  2221. * To save a fully independent HeliOS executable
  2222.  
  2223.   1. Load and compile your program, remembering that the last word in the
  2224.      program will automatically be run when your code is re-executed in a
  2225.      standalone environment.
  2226.  
  2227.   2. Select the "Compile standalone" Interpreter menu option.
  2228.  
  2229.   3. Select the "Presave" Interpreter menu option to save the executable.
  2230.  
  2231.   4. Execute your program using the simple AmigaDOS command line:
  2232.  
  2233.      MyProgram
  2234.  
  2235.  
  2236. N.B.
  2237.  
  2238. Remember to re-select the "Compile dictionary" Interpreter menu option
  2239. when you wish to return to saving and loading full Interpreter-based
  2240. HeliOS programs.
  2241.  
  2242.  
  2243. * An important point to remember....
  2244.  
  2245. There is one final point of interest here which you will find very
  2246. useful when compiling compact HeliOS program disks.  The "HeliOS_exe"
  2247. file is an ordinary AmigaDOS executable file which is provided on your
  2248. HeliOS master disks in uncompressed form.  If you wish, you may use a
  2249. packer such as PowerPacker to compress this executable into a file
  2250. somewhat less than half the size of the original, thus enabling you to
  2251. save space on any of your HeliOS program disks which run HeliOS overlay 
  2252. files using HeliOS_exe.
  2253.  
  2254. Please note however that the copy of "HeliOS_exe" which resides in your
  2255. master "HeliOS:" directory MUST NOT BE PACKED.  This is because this file
  2256. is used in its UNPACKED form by the main HeliOS Interpreter program when
  2257. it is creating standalone executables.  If you pack the "HeliOS_exe" file
  2258. which is in the HeliOS: directory you will find that all standalone HeliOS 
  2259. programs created from the Interpreter will not work at all.
  2260.  
  2261.  
  2262. ----------------
  2263. The HeliOS Stack
  2264. ----------------
  2265.  
  2266. As mentioned above, the stack is a place where HeliOS stores numbers
  2267. on a temporary basis.
  2268.  
  2269. It is rather as if HeliOS were to say to the stack "Here you are, please
  2270. hold these for me while I go and do something else!  Please give them to
  2271. the next person who asks for them.".
  2272.  
  2273. The stack, as its name implies, is rather like a pile of data items,
  2274. and each new item simply goes onto the top of the pile/stack.
  2275.  
  2276. Look at this simple diagram:
  2277.  
  2278. a  <---- 1st item or "Top of stack item"
  2279. b  <---- 2nd item
  2280. c  <---- 3rd item
  2281. d  <---- 4th item
  2282.  
  2283. etc. etc.
  2284.  
  2285. Let us put just one number, the number 4, on the stack:
  2286.  
  2287. 4  <--- Top of stack
  2288.  
  2289. Now let us put just another number, the number 8, on the stack:
  2290.  
  2291. 8  <--- Top of stack
  2292. 4  <--- 2nd place on stack
  2293.  
  2294. Notice how each new number becomes the "top" number and displaces any
  2295. other numbers downwards.
  2296.  
  2297. If a computer, which is a simple minded creature, wants to do something
  2298. like an addition sum, it cannot do this quite so flexibly as we can.
  2299.  
  2300. Faced with the problem of adding 4 to 8 to give 12, we clever humans
  2301. might think something like this.
  2302.  
  2303. "Ah, first I see the number 4: lets see what we need to do with it."
  2304.  
  2305. "Oh, I see a "+" sign, so I need to find another number to add."
  2306.  
  2307. "There it is: 8.  I will add them together. The result is 12."
  2308.  
  2309. "Whether I write 12 down or just remember it, I KNOW the answer was 12".
  2310.  
  2311.  
  2312. Or we might look at it differently, like this:
  2313.  
  2314.  
  2315. "Ah, two numbers 4 and 8."
  2316.  
  2317. "What is it that I have to do with them?"
  2318.  
  2319. "Ah, I see, a "+" sign, so I will add them together to get 12"
  2320.  
  2321. "Whether I write 12 down or just remember it, I KNOW the answer was 12".
  2322.  
  2323.  
  2324. Or we might look at it differently again:
  2325.  
  2326. "Ah, a "+" sign."
  2327.  
  2328. "This needs two numbers to add, so what are they?"
  2329.  
  2330. " I see, 4 and 8, that gives 12."
  2331.  
  2332. "Whether I write 12 down or just remember it, I KNOW the answer was 12".
  2333.  
  2334. You might think all these are boringly similar and obvious, but a computer
  2335. does not even have this limited choice.
  2336.  
  2337. Putting it simply:
  2338.  
  2339. *   A computer has NO choices about how to carry out an operation.
  2340.  
  2341. *   A computer needs to have its DATA loaded FIRST into its processing
  2342.     unit's data storage space.
  2343.  
  2344. *   A computer carries out an operation like "+" by ASSUMING that data
  2345.     has ALREADY been stored in its processing unit data storage space.
  2346.  
  2347. *   A computer has no automatic "COGNITIVE MEMORY" or "KNOWLEDGE".
  2348.  
  2349. *   A computer simply places the result of its calculation in its
  2350.     processing unit's data storage space, then carries on and forgets
  2351.     what has happened.
  2352.  
  2353. So, to perform and store an addition, a computer program needs to:
  2354.  
  2355. 1. Get the first number
  2356. 2. Get the second number
  2357. 3. Add them together
  2358. 4. Store the result
  2359.  
  2360. The computer is quite inflexible and can work in no other way.
  2361.  
  2362. If you write an addition "sum" like this:
  2363.  
  2364. 4 + 8
  2365.  
  2366. and pass it to the computer (as some computer languages do) the computer
  2367. has to be made to carry out various extra manipulations INTERNALLY by the
  2368. computer language BEFORE it can do the calculation in the form it can work
  2369. with.  This is slowing everything down because more work is having to be
  2370. done UNNECESSARILY by the computer.
  2371.  
  2372. This is something like what the computer language would have to do
  2373. internally, unseen by you:
  2374.  
  2375. 1. Read the first number.
  2376.  
  2377. 2. It does not know what to do with it yet, so it stores it.
  2378.  
  2379. 3. Read the "+" command.
  2380.  
  2381. 4. It looks up the "+" command to see what this command does when it
  2382.    executes.
  2383.  
  2384. 5. It sees that two numbers are required for this command.
  2385.  
  2386. 6. It stores or "remembers" that it is about to do a "+" operation.
  2387.  
  2388. 7. It gets the second number and stores it.
  2389.  
  2390. 8. It goes back to recall what command it was performing.
  2391.  
  2392. 9. It performs the addition.
  2393.  
  2394. On the other hand, if YOU supply the computer with its data in the form
  2395. in which it can handle it directly, you get much faster operation.
  2396.  
  2397. If you write the "sum" above like this:
  2398.  
  2399. 4 8 +
  2400.  
  2401. look how easy it is for the computer.
  2402.  
  2403. 1. Get a number onto the stack
  2404.  
  2405. 2. Get a number onto the stack
  2406.  
  2407. 3. Execute the "+" command, which takes two numbers off the stack and
  2408.    replaces the result on the stack.
  2409.  
  2410. The operation is over quickly and efficiently, the computer can go on
  2411. to do something else, and the result is automatically "remembered" on
  2412. the stack, where YOU can do something with it.
  2413.  
  2414. This means that computers can work more efficently, in general, by being
  2415. given numbers first, followed by function commands.
  2416.  
  2417. This is how HeliOS always works, because we are only interested in maximum
  2418. speed and efficiency: wherever the programmer can save the computer extra
  2419. work the program will run quicker.
  2420.  
  2421. The stack is a nice quick and simple "universal" storage area which is
  2422. organised internally in such a way the HeliOS can feed the computer's
  2423. central processing unit directly from the stack very quickly indeed.
  2424.  
  2425. The method of putting numbers on the stack BEFORE the operator function
  2426. is performed is called POSTFIX notation or REVERSE_POLISH notation.
  2427.  
  2428. The more usual "human" method of putting the operator IN-BETWEEN the number
  2429. parameters is called INFIX notation.
  2430.  
  2431. HeliOS uses postfix notation rather than infix notation for another good
  2432. reason besides the one mentioned above.
  2433.  
  2434. Much of the power of HeliOS is derived from the fact that postfix notation
  2435. allows a very elegant and simple mechanism for passing parameters between
  2436. command functions.
  2437.  
  2438. Look at these examples:
  2439.  
  2440. The function "+" gets two numbers from the stack and adds them.
  2441.  
  2442. The function "FPENSET" gets one number from the stack and sets a pen colour.
  2443.  
  2444. The function "CURPUT" gets two numbers from the stack and sets the cursor.
  2445.  
  2446. So we can have a word like this:
  2447.  
  2448. : FANCYPRINT 12 + CURPUT FPENSET ." Hello!" ;
  2449.  
  2450. with a stack diagram   ( Colour, Column, RowOffset - - - )
  2451.  
  2452. Look what happens here:
  2453.  
  2454. The function FANCYPRINT will expect three numbers already on the stack:
  2455.  
  2456. RowOffset  <- Top of stack
  2457. Column     <- 2nd on stack
  2458. Colour     <- 3rd on stack
  2459.  
  2460. It then puts the number 12 on TOP of the others:
  2461.  
  2462. 12         <- Top of stack
  2463. RowOffset  <- 2nd on stack
  2464. Column     <- 3rd on stack
  2465. Colour     <- 4th on stack
  2466.  
  2467. It then adds 12 to the RowOffset value, giving a cursor displacement
  2468. offset from "Row 12", which is where we want to place our text.
  2469.  
  2470. This "final" calculated Row value is placed back on the stack:
  2471.  
  2472. RowOffset+12 (= Calculated Row position) <- Top of stack
  2473. Column                                   <- 2nd on stack
  2474. Colour                                   <- 3rd on stack
  2475.  
  2476. The function CURPUT is then executed, and we know that CURPUT takes
  2477. two numbers from the stack and places the cursor at that position.
  2478.  
  2479. The stack now looks like this:
  2480.  
  2481. Colour                                   <- TOP of stack
  2482.  
  2483. The function FPENSET is then executed, and we know that FPENSET takes
  2484. one number from the stack and sets the text colour.
  2485.  
  2486. Finally the ." function prints the following text.
  2487.  
  2488. Look at this carefully to see how simply the parameter passing works, and
  2489. then try to work out how it might work with INFIX notation.
  2490.  
  2491. You will probably agree that although it may be initially rather "strange"
  2492. compared to our habitual "human" use of INFIX notation, POSTFIX notation
  2493. is a great way of controlling a computer program!
  2494.  
  2495. Since HeliOS is concerned with helping you control your Amiga in the most
  2496. efficient way possible, you are going to HAVE to do your share of the work
  2497. and learn to use the stack and POSTFIX notation.
  2498.  
  2499. You will find this really very easy after the first few HeliOS programming
  2500. sessions, and you will notice that the HeliOS Interpreter has a "real-time"
  2501. permanent stack display to help you get used to how the stack operates.
  2502.  
  2503. Try doing some simple arithmetic operations in the Interpreter and watch
  2504. the way the stack display changes.  Stick with this until you really are
  2505. confident that you have grasped the way the stack works, because this
  2506. understanding is absolutely vital to everything you will do in HeliOS.
  2507.  
  2508. You may find it useful to try these simple command words:
  2509.  
  2510.   +        Adds (and removes) the top two numbers on the stack, then puts
  2511.            the result back on the stack
  2512.  
  2513.   -        Subtracts (and removes) the top two numbers on the stack, then
  2514.            puts the result back on the stack
  2515.  
  2516.   .        Prints the number on top of the stack and removes it
  2517.  
  2518.   .s       Prints all the stack contents without changing anything
  2519.  
  2520.  DUP       Duplicates the top stack number
  2521.  
  2522.  SWAP      Exchanges the top two stack numbers
  2523.  
  2524.  ROT       Moves the 3rd number on the stack to the top
  2525.  
  2526.  OVER      Duplicates the 2nd number on the stack on top of the stack
  2527.  
  2528. These last four words are called "stack manipulation" words.
  2529.  
  2530. There are many of these stack manipulation words in the main HeliOS CORE
  2531. vocabulary.
  2532.  
  2533. Look at the "Dictionary.doc" now and read the notes concerning these words,
  2534. then try using a variety of them to become accustomed to the way the stack
  2535. can be manipulated.
  2536.  
  2537. Having done this you will have had some experience of what is called "stack
  2538. management".
  2539.  
  2540. Stack management is very important in HeliOS, and you can imagine that in
  2541. any language which uses stack parameter passing you will very often have
  2542. the need to adjust and modify the stack.
  2543.  
  2544. This can actually be quite interesting and often even becomes addictive,
  2545. in the manner of a puzzle, with many programmers taking delight in managing
  2546. very complex stacks.
  2547.  
  2548. Which brings us to another VERY important rule:
  2549.  
  2550. * Do NOT attempt to work with large stack sizes!
  2551.  
  2552. The stack is designed for quick and simple parameter passing, and if you
  2553. find that things are getting complicated you should either break down your
  2554. program into simpler more manageable chunks or resort to other techniques
  2555. such as using named constants and variables.
  2556.  
  2557. We will explain constants and variables later, but basically these allow
  2558. you to manipulate numbers using names rather than simply referring to a
  2559. position on the stack.  You will find that in some cases the stack is more
  2560. efficient, and sometimes using constants and variables is better.
  2561.  
  2562. You will soon learn your own programming style and preferences, but you
  2563. should NOT try to avoid using the stack by using methods you might be
  2564. accustomed to from other programming languages: the stack is a dynamic
  2565. and efficient programming tool, and MUST be used properly.
  2566.  
  2567. Finally here are two more terms which you will find used in reference
  2568. to the stack:
  2569.  
  2570. "Stack overflow"
  2571.  
  2572. This means that there are too many numbers on the stack.
  2573.  
  2574. "Stack underflow"
  2575.  
  2576. This means that there are too few numbers on the stack for a given function.
  2577.  
  2578. **************************************************************************
  2579. End
  2580. **************************************************************************
  2581.